Re: Redisplaying information from a HTML form

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux