I'm trying to make a form I've created sticky.
I used a for loop to create some lengthy select options, but it can't seem to figure out a way to make it sticky.
Here is one of the loops I've created:
<p>Ave. Speed: <select name="speed" id="speed">
<option value="">Speed</option>
<?php //Print out speed
for ($s = 70; $s <=105; $s++) {
print "<option value=\"$s\">$s
</option>
";
}
?>
</select></p>
How can i do this ?
A couple of assumptions here, i.e. you are using POST to send your form data and no processing has been done on $_POST['speed']
yet. Check whether the value of $_POST['speed']
posted matches the value of $s
and then use selected
in your option
tag:
<p>Ave. Speed: <select name="speed" id="speed">
<option value="">Speed</option>
<?php
//Print out speed
for ($s = 70; $s <=105; $s++) {
echo "<option value=\"$s\"";
if($_POST['speed']==$s){echo' selected="selected"';}
echo">$s</option>
";
}
?>
</select></p>
I figured it out!
<select name="speed" >
<option value="">Speed</option>
<?php //Print out speed
for ($s = 70; $s <=105; $s++) {
echo "<option value=\"$s\"";
if (isset($_POST['speed']) && $_POST['speed'] == $s){
echo 'selected="selected"';
} //end if
echo ">$s</option>
";
}
?>
</select>
I just needed to check for 2 conditions. One for the option selected, and the other equal to the temporary variable.