You are here: Re: [PHP] Redisplaying information from a HTML form « PHP « IT news, forums, messages
Re: [PHP] Redisplaying information from a HTML form

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация