Grow your CSS skills. Land your dream job.

Remember Dropdown Selection

  • # November 16, 2010 at 4:58 pm

    Does anyone know of some PHP to remember a form’s dropdown selection on submit?

    At the moment I’m using this to remember a selection:




    But when you have 10+ options it can get a bit much, is there a simpler way of doing this?

    # November 16, 2010 at 6:30 pm

    There is no real need to post this twice, albeit with one word changed in the topic from PHP to JavaScript

    You could use an array and a foreach statement to save yourself a lot of time

    e.g.


    $arOptions = array('100', '250', '500', '1000');

    foreach ($arOptions as $strOption){
    echo '
    if ($strOption == $_GET) echo ' selected ';

    echo '';

    }
    # November 16, 2010 at 6:57 pm

    Sorry, I wasn’t sure what the topic fell underneath. Is there a way of making all dropdowns remember their selection on submit without repeating that ^.

    # November 16, 2010 at 8:03 pm

    If you don’t want to clutter the URL:

    Set cookie on submit script
    Read cookie on new page
    Use similar code to Mark’s only you just already have a variable set from the Cookie read, you don’t need to $_GET it.

    # November 17, 2010 at 6:45 am

    I’ve found some script (below) which is working for one of the Select Lists (MinPrice). I also need to target the second Select List (MaxPrice), how would I do that?



    Rob
    # November 17, 2010 at 7:13 am

    Just to look at your original code – you can use a shorthand if/else like this…



    and the second part – remember to close your first option tag – personally I like to build a var and echo it. You can also use number_format to set how you want your number to float.



    $arOptions = array('100', '250', '500', '1000');

    foreach ($arOptions as $value){

    $e = "
    $e .= ($value == $_GET) ? " selected " : "";

    $e .= ">£" . number_format($value, 2, '.', ''); . "";

    echo $e;
    }
    # November 17, 2010 at 7:29 am

    The array works perfectly for one part of the form (Buying), but when you switch the radio button to Renting it calls its JavaScript and pulls its select options from that. I can’t seem to work out a way to use that array in the JavaScript (below):


    document.SearchForm.MaxPrice.options[0]=new Option ("Price (Max)","9999999");
    document.SearchForm.MaxPrice.options[1]=new Option("u00A3250","250");
    document.SearchForm.MaxPrice.options[2]=new Option("u00A3500","500");
    document.SearchForm.MaxPrice.options[3]=new Option("u00A3600","600");

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".