treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Remembering Selection in Select List

  • I'm using this bit of code below for a Select List, as you can see I've got a bit of PHP between each option that remembers the selection when the page is reloaded.

    MY HTML:

    <select name="MaxPrice" id="MaxPrice">
    <option value="9999999" selected>Price (Max)</option>
    <option value="100000" <?php if($_GET['MaxPrice'] == "100000") { echo("selected"); } ?>>&pound;100,000</option>
    <option value="200000" <?php if($_GET['MaxPrice'] == "200000") { echo("selected"); } ?>>&pound;200,000</option>
    </select>

    Unfortunately I need to scrap the code (above) and use the code below (JavaScript). The problem is I need the selection to be remembered, exactly as it is above. So when the page is reloaded the users selection on the list doesn't go away/reset. How can I achieve this in the code below?

    MY JAVASCRIPT:

    if (BuyRent=='buy')
    {
    document.SearchForm.MaxPrice.options[0]=new Option("Price (Max)","150000000");
    document.SearchForm.MaxPrice.options[1]=new Option("\u00A3100,000","100000");
    document.SearchForm.MaxPrice.options[2]=new Option("\u00A3200,000","200000");
    }


    Thanks in advance.
  • use cookies.

    you could use setcookie() function in php. when the user has made the selection set the cookie using that function, then when the page is reloaded/redirected, just read the cookie values and apply it.