• I created a user form on my index page which allows the site visitor to re-order my post loop order based on either most recent posts or highest rated posts. The form works perfectly, the problem is: the post loop loads by most recent as a default, but after they select/submit the highest rated order, the page refreshes and the dropdown list displays the default option value of “Most Recent Posts” when it should be displaying the current option value of “Highest Rated Posts”. This is confusing for the reader because it appears as if they are still seeing the most recent when they are in fact seeing the highest rated.

    How can I force the dropdown list to stay on the selected option instead of defaulting back to first option after refresh?

    Here is my code:

    <?php $sort= $_GET['sort']; if($sort == "date") { $order= "orderby=date"; } if($sort == "highest_rated") { $order= "orderby=meta_value_num"; } ?>
    
    <form class="formindexfilter" action="#ifilter" method="get">
    <input type="hidden" name="r_orderby" value="desc">
    <select name="r_sortby" id="sorting">
     <option value="date" <?php if ($sort == "date") { echo ' selected="selected"'; } ?> >Most Recent Posts</option>
     <option value="highest_rated" <?php if ($sort == "highest_rated"){ echo ' selected="selected"'; } ?> >Highest Rated Posts</option>
    </select>
    <input type="submit" value="Submit" />
    </form>

    I also tried doing it like this:

    <option selected="selected" value="date" <?php if ($sort == "date");?> >Most Recent Posts</option>
     <option selected="selected" value="highest_rated" <?php if ($sort == "highest_rated");?> >Highest Rated Posts</option>

    However this makes the highest rated option value always selected, while the earlier code makes most recent always selected. What am I doing wrong? I just want the option value to accurately describe the loop results all the time.

    Any help would be extremely appreciated.

  • The topic ‘Dropdown List Won't Display Selected Option After Submit / Refresh’ is closed to new replies.