How do I retain form values upon submit for a loop?
I’ve got something like this:
<?php
printf ("<optgroup label=\"CATEGORIES\">");
foreach ($category as $key => $val) {
printf ("<option value=\"$key\">$val</option>");
}
printf ("</optgroup>");
?>
When hitting submit on another part of my form, it resets the values in the select to the top one. I want it to keep the same value. How? For things like input boxes I have this that works:
<option value="5" name="results" <?php if($_GET["results"] == '5'){ ?> selected="selected" <?php } ?> >5</option>
This works because I’m not dynamically populating the dropdown from a database, so I don’t need a loop to loop through values and populate the select.
1 Answer