View Full Version : keeping selection in drop down box after submit form

11-07-2007, 05:36 PM
I have a form which validates on the same page with error messages coming up if information has been inputed incorrectly. The problem is that if radio and dropdown selection has been chosen but an error message comes up for another field the selection the dropdown selection and radio box check does not keep its value. so far i have this

<option value ="$_POST[exercise]"></option>
<option selected></option>
<select name ="exercise">
<select name ="exercise">

$options = array('',0,2,3,4,5,6,7,;
foreach ($options as $option)
echo '<option value="'.$option.'"';
if (isset($_POST['exercise'])&&$_POST['exercise']==$option) echo ' selected';
echo '>'.$option.'</option>';
</select> hours
<font size="2" color="red"><?php print "$exmessage" ?></font><p>

this keeps the selection but only prints the last character in the drop down list - so you can only see number 8.
I have this

<option value=" " <?php (isset($_POST[exercise]) && $_POST[exercise] == ' ')? ' selected ':null ?>> </option>
<option value="0" <?php (isset($_POST[exercise]) && $_POST[exercise] == '0')? ' selected ':null ?>>0</option>
<option value="1" <?php (isset($_POST[exercise]) && $_POST[exercise] == '1')? ' selected ':null ?>>1</option>
<option value="2" <?php (isset($_POST[exercise]) && $_POST[exercise] == '2')? ' selected ':null ?>>2</option>
<option value="3" <?php (isset($_POST[exercise]) && $_POST[exercise] == '3')? ' selected ':null ?>>3</option>
<option value="4" <?php (isset($_POST[exercise]) && $_POST[exercise] == '4')? ' selected ':null ?>>4</option>
<option value="5" <?php (isset($_POST[exercise]) && $_POST[exercise] == '5')? ' selected ':null ?>>5</option>
<option value="6" <?php (isset($_POST[exercise]) && $_POST[exercise] == '6')? ' selected ':null ?>>6</option>
<option value="7" <?php (isset($_POST[exercise]) && $_POST[exercise] == '7')? ' selected ':null ?>>7</option>
<option value="8" <?php (isset($_POST[exercise]) && $_POST[exercise] == '8')? ' selected ':null ?>>8</option>

which doesn't keep the selection selected and does not seem to bring up the error message when nothing selected.

does anyone know what im doing wrong? i know its probably a simple-ish problem but i have been stuck for days on this so am really hoping someone will reply many thanks :)

11-07-2007, 06:32 PM
Try something like this:

$options = array('',0,2,3,4,5,6,7,8);
foreach ($options as $option)
echo '<option value="' . $option . '"' . (isset($_POST['exercise']) && $_POST['exercise'] == $option ? ' selected' : '') . '>' . $option . '</option>';

11-08-2007, 01:29 AM
Thanks Inigoesdr
That works wonders:) i was trying with ternary operators for a while but didn't quite get them right.

is there a chance you could check this out for me? its a gender radio box and it works fine by keeping the radio checked when checking validation but i am trying to make an if saying if female then do one thing else.. blah. but when i print $gender it stays at male no matter what i do! i was thinking of using the (isset($_POST['gender']) in the below code but can't seem to get the syntax right. sorry i am a real newbie but with this massive learning curve i will be a master by next month;)

<input type="radio" name="gender" value="male" <?=($gender=='male')? 'checked' : '' ?> >male
<input type="radio" name="gender" value="female" <?=($gender=='female')? 'checked' : '' ?> >female

11-08-2007, 05:02 AM
<input type="radio" name="gender" value="male" <?=($gender=='male')? 'checked' : '' ?> >male
<input type="radio" name="gender" value="female" <?=($gender=='female')? 'checked' : '' ?> >female
When you have short tags on and you use that <?= syntax that tells PHP to echo the command that follows it, but if you want that code to work properly you need to wrap the rest of the commands in parenthesis:

<input type="radio" name="gender" value="male" <?=($gender == 'male' ? 'checked' : ''); ?> >male
<input type="radio" name="gender" value="female" <?=($gender == 'female' ? 'checked' : ''); ?> >female
Keep in mind though, that if you intend to distribute this code to servers you don't control, some people don't have short tags on. :thumbsup: