www.webdeveloper.com
Results 1 to 3 of 3

Thread: Save DropDown selection with HTML5 storage?

  1. #1
    Join Date
    Jan 2012
    Posts
    19

    Save DropDown selection with HTML5 storage?

    hey everyone,

    I have;

    <form method="post" action="search.php">
    <select name="country" class="dropdownselect" >

    <option value="">Select Country</option>

    <option value="Afghanistan" selected="selected">Afghanistan</option>
    <option value="Albania" selected="selected">Albania</option>
    <option value="Algeria" selected="selected">Algeria</option>
    <option value="American Samoa" selected="selected">American Samoa</option>
    </select>

    </form>

    And then Im doing SQL query to display results from database.


    I need to somehow leave selected option selected after someones preform search and pages refresh.

    Im planing to use HTML5 storage, something like this;

    <script>

    localStorage.setItem('country', $('select[name=country]').val());
    localStorage.getItem('country');

    </script>

    But I dont know how to display it correctly,


    Can someone help?



    Thanks in advance!


    Josip

  2. #2
    Join Date
    Mar 2011
    Posts
    1,145
    Setting the 'selected' attribute on all of the <option> tags in a <select> list is invalid, so be sure to remove those attributes before you do anything else.

    I think a better method would be to have the PHP script that generates this page set the 'selected' attribute if it's executing a user search (as opposed to displaying the page for the first time). This avoids the HTML5 incompatibility issues with older browsers and avoids issues with users running with cookies disabled or blocked (which is also a good alternative).

    So somewhere near the top of your PHP code, you'd have:
    Code:
     $countrySelected = '';
     if (isset($_POST['country'])) { $countrySelected = $_POST['country']; }
     ?>
    and then later when you're displaying the search form's <select> list of country <options>:
    Code:
    <select name="country" size="1">
    <?php
    .
    .
     echo("<option value=\"$countryName\"");
      if ($countryName == $countrySelected) { echo(" selected=\"selected\"); }
     echo(">$countryName</option>");
    .
    .
    ?>
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Aug 2012
    Location
    1453 Gingerwood ave, Milpitas CA 95035,
    Posts
    37
    I was also facing the same problem... thanx for putting this prob here & taking out the solution........

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles