#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    PHP Drop-down list retain data when submitted (Posting to same page)


    Hi guys,

    I have a drop-down list that has several options. When the user selects an option and clicks on submit, the page reloads and displays the correct data in a table below the drop down list. This all works perfectly, however I would like the drop-down list to display the selected value. I.e. if the user selects "Active users" from the drop-down list, I would like it to keep "Active users" in the drop-down list when the page reloads and is showing the relevant table. Thanks in advance guys!

    here is my code:

    Code:
    <form action="admin.php" method="post"> <label for="admin_users">Users:</label> <div class="search_admin"> <select name="admin_search"> <option value="users">All Users</option> <option value="active_users">Active Users</option> <option value="disabled_users">Disabled Users</option> <option value="band4">30 - 39</option> </select> </div> <div class="search"> <input type="submit" value="Search"> </div> </form>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Kurvi
    Posts
    490
    Rep Power
    185
    In short:
    Code:
    <?php
    $selectedValue = empty($_POST["admin_search"] ? "" : $_POST["admin_search"] );
    ?>
    <select name="admin_search"> 
       <option value="users" <?php echo ($selectedValue == "users" ? "SELECTED" : ""); ?> >All Users</option>
       ...
    Gabba Gabba Hey
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Thanks very much mate. This worked after I added in another set of () when setting the variable. Thanks again.

IMN logo majestic logo threadwatch logo seochat tools logo