|
Posted by Torgny Bjers on 08/11/05 16:00
Ravi Gogna wrote:
[snip]
> I've managed to write the checking program in such a way that clicking
> submit launches an 'error' page which displays at the top of the page
> which field is wrong, and then redisplays the form. (The form
> redisplay is done using a function which uses the variables I used in
> the HTML form page). My problem is this: when the 'error' page comes
> up all of the text boxes will quite happily redisplay the data that
> was put into them, but I have a couple of drop-down boxes and radio
> buttons which lose their value. Is there a way I can make these boxes
> and buttons retain their value?
The easiest way to do this is to keep all the select option items in an
associative array and iterating this with foreach or for to output the
<option/> elements. That way you can directly check the value of each
option to detect which was selected.
MailerCode(tm):
<?php
$select = array('a' => 'Item 1', 'b' => 'Item 2', 'c' => 'Item 3');
?>
<form ...>
<select name="MySelect" id="MySelect">
<?php
foreach ($select as $value => $text) {
$selected = '';
if (!empty($_POST['MySelect']) && $_POST['MySelect'] == $value) {
$selected = ' selected';
}
printf('<option value="%s"%s>%s</option>%s', $value, $selected,
$text, "\r\n");
}
?>
</select>
</form>
Warm Regards,
Torgny
[Back to original message]
|