Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Remember Dropdown Selection

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #30708
    realph
    Participant

    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?

    #75049
    TT_Mark
    Member

    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 '';

    }
    #75022
    realph
    Participant

    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 ^.

    #75025
    Chris Coyier
    Keymaster

    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.

    #74968
    realph
    Participant

    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?



    #74780
    Rob MacKay
    Participant

    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;
    }
    #74782
    realph
    Participant

    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)
  • The forum ‘Back End’ is closed to new topics and replies.