Oops. I actually tried running it (cleverly waiting until after I posted
it to the list) and I see that there are some warnings in the event that
nothing has been selected (due to in_array operating on an undefined
variable). Don't worry about them. Or change that last foreach clause to
look like this:
foreach ($choices as $id => $name)
print '<option '
. (is_array($_REQUEST['animals'])
&& in_array($id, $_REQUEST['animals']) ? 'selected ' : '')
. "value='$id'>$name</option>";
(I added an is_array to make sure we don't send a null value as the second
argument to in_array).
miguel
On Sat, 25 May 2002, Miguel Cruz wrote:
> Have a play with this little program - it should demonstrate all of the
> things you'd want to do with getting data in and out of mult selects. If
> it doesn't work, you may have an older version of PHP - in that case,
> change "$_REQUEST" to "$HTTP_POST_VARS" and "$_SERVER['PHP_SELF']" to
> "$PHP_SELF".
>
> ---------------
> <?
>
> $choices = array(1 => 'dog', 2 => 'cat', 3 => 'mouse', 4 => 'frog');
>
> if (is_array($_REQUEST['animals']))
> {
> print '<p>You chose:</p><ul>';
> foreach ($_REQUEST['animals'] as $animal_id)
> print "<li>{$choices[$animal_id]}</li>";
> print '</ul><hr>';
> }
>
> ?><form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
> <select multiple name="animals[]"><?
>
> foreach ($choices as $id => $name)
> print '<option '
> . (in_array($id, $_REQUEST['animals']) ? 'selected ' : '')
> . "value='$id'>$name</option>";
>
> ?></select><input type="submit"></form>
>
> ---------------
> miguel
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php