Re: Need some advice concerning forms (multi select pulldown) and arrays

From: Date: Sat, 25 May 2002 19:08:01 +0000
Subject: Re: Need some advice concerning forms (multi select pulldown) and arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-99254@lists.php.net to get a copy of this message
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 > > >

« previous php.general (#99254) next »