Re: Return value in Combo Box

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

 



Ahmed Abdel-Aliem wrote:
Hi, i have a form which user have to fill all the fields in it, when the
form is submitted it goes to a validation page which checks if users
entered all the required fields.


what i want to do is to make the validation page redirect to the form
again with the previuosly entered values, so the user can fill the
missing fields.

i can do this easily with input texts and text areas, but i don't know
how to do it with select and combo boxes.

can anyone please tell me how to make the choice the user selected in
the combo box be selected when the validation page redirects to the
form again ?

What I find usually happens with selects is that I am selecting the option value and the display value from a table and displaying them in a loop, in this loop you can check if the post value is the same as the option value, and make it selected if it is. For example:


<?php

$People = pg_exec($DBConnection, 'SELECT id, first, last FROM people');
$NumPeople = pg_numrows($People);
echo '<select name="Person">';
for($i = 0; $i < $NumPeople; $i++){
    $ID = pg_result($People, $i, 0);
    $First = pg_result($People, $i, 1);
    $Last = pg_result($People, $i, 2);

    echo '<option value="' . $ID . '"';
    if($_POST['Person'] == $ID){ echo ' selected="selected"'; }
    echo '>' . $First . ' ' . $Last . '</option>';
}
echo '</select>';
?>

The key part here is the
if($_POST['Person'] == $ID){ echo ' selected="selected"'; }
which will make the previously selected option selected again when the form is redisplayed. This also works for checkboxes, but I think checkboxes need checked="checked".


-- David Dickson

--
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