I have made a filter using a form with drop down lists. The filter is used to filter results from a mySQL database. The form is processed on the same page as the results. I want to make it so that the filter drop down lists retain the filter parameters once the results have been filtered.
I have done this in the past by using a session, however that was when I used another page to process the query. I have tried to apply this same theory but it isn’t working. I have searched this site as well as Google and I have only found answers related to a separate page for processing.
Currently the code that I have will only pre-populate the form after it has been submitted twice. The first time it is submitted it shows the default setting of “Select…”
Here are the relevant sections of my code:
session_start();
if(isset($_SESSION['submitted_filter_values']))
{
extract($_SESSION['submitted_filter_values']);
}
<form action="#filter" method="post" name="filter">
<table>
<tr>
<td>
<select name="prefix">
<option value="">Select...</option>
<option <?php if(isset($prefix) && $prefix == "PDM"){echo "selected=\"selected\"";} ?> value="PDM">PDM</option>
<option <?php if(isset($prefix) && $prefix == "TB"){echo "selected=\"selected\"";} ?> value="TB">TB</option>
<option <?php if(isset($prefix) && $prefix == "JNL"){echo "selected=\"selected\"";} ?> value="JNL">JNL</option>
</select>
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" class="btn_150x30" name="submit" value="Filter Results"></td>
</tr>
</table>
</form>
$_SESSION['submitted_filter_values'] = $_REQUEST;
I have tried placing the session variable in different locations but that didn’t work. I thought maybe refreshing the page would work but then I figured it would effectively remove the filter like it does when I submit the form twice. Interesting point to note is that is it very consistent in that it works literally every second time I submit it, so every odd submit it does not populate and every even submit it populates.
After realizing that the session wasn’t a good idea for my application, because it is only being used on one page and not throughout the site, I have taken a fresh approach to the problem.
At the top of my filter I have this for each option:
Then in my drop down list, I have this for each option: