Forum Moderators: coopster

Message Too Old, No Replies

printing or echoing option selected by user

         

Shaman13

5:19 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



I know this has probably been answered a thousand times, but could someone please answer it for the 1001st? I have a select list which allows the user to make a choice. After the user clicks submit his choice disappears. Which means if he or she wants to search again they must reselect their choice. Is there a way to print or echo the selection even after submission is made. The following is my select code. All suggestions,comments are really appreciated! Thanks for answering this newby question yet another time.

<select name="FUNDS">
<option value="1">Red 1</option>
<option value="2">Blue 2</option>
<option value="3">Green 3</option>
<option value="4">Yellow 4</option>
</select>

Zipper

6:38 pm on Feb 1, 2005 (gmt 0)

10+ Year Member




$funds = array("Red 1", "Blue 2", "Green 3", "Yellow 4");
$n_funds = count($funds);
echo "<select name=\"FUNDS\">";
for($i=1;$i<=$n_funds;$i++){
echo "<option value=\"$i\" ";
if($i == $_POST['FUNDS']){ //<--use the proper method
echo "selected";
}
echo ">".$funds[$i-1]."</option>\n";
}
echo "</select>";

pretty sure there's a better way though..

coopster

12:07 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Spot on. Loop through to build the options list and compare each option to the possible value(s) submitted by the user in the form to see if there is a match and just set that option to selected. You might also want to check to make sure the method is actually set before building the list and make sure the user didn't try to spoof a form entry on you:
if (!isset [php.net]($_POST['FUNDS']) 
or !in_array [php.net]($_POST['FUNDS'], $funds)) {
// we have an invalid submission
} else {
// all is well, continue processing...
}