Click to See Complete Forum and Search --> : Looping through pull-down menu (<select>)


Net apprentice
04-12-2004, 10:46 PM
Well, my first post in your forum...

Here is a rather ordinary question: how do I loop through my pull-down menu (multiple select within a form) to address multiple selected items. Right now with $_POST['items'] I get the value of the last selected item. Here's a piece of my code:

<?php
if(isset($_POST['submit'])){
echo ("<p>Items: ". $_POST['items']."</p>");
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

<label for="items">Items:</label>
<select name="items" size="3" class="textarea" multiple>
<option value="item1">item 1</option>
<option value="item2">item 2</option>
<option value="item3">item 3</option>
</select>
<input name="submit" type="submit" value="Send">&nbsp;
<input name="reset" type="reset" value="Reset">
</form>

I am sure there must be some pretty simple way to do this.
Any ideas?

Jona
04-12-2004, 10:56 PM
I don't know about PHP5, but the version I have (4.3.5 I think) makes you name it as an array. See an example. (http://cmm.sonoracabinets.com/testing/selAry.php)


<?PHP
if(!isset($_GET["node"]) || $_GET["node"] != "process" && !isset($_POST["submit"])){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head><title>Testing with Select Boxes and Arrays</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<form action="selAry.php?node=process" method="POST"><div>
<select size="4" name="selObj[]" multiple="multiple">
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
</select><br>
<input type="submit" name="submit">
</div></form>
</body></html>
<?} else {?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head><title>Testing with Select Boxes and Arrays</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<p><?
$v = "";
foreach($_POST['selObj'] as $value) {
$v .= $value;

for ($i=1; $i<5; $i++) {
echo $i.". (";
echo $value.") ";
$pos = split($value, $i-1);
if($pos == $i){echo "No<br>";}
else{echo "Yes<br>";}
}
};
?></p>
</body></html>
<?}?>

Net apprentice
04-12-2004, 11:21 PM
Thanks for a quick reply.
That's what I was looking for.

Spasibo! ('Thank you' in Russian)

P.S. Can we determine the length of an array (to use instead of $i<5; in our 'for' conditions)?

Jona
04-12-2004, 11:26 PM
Sorry I made it a long time ago.


$length = count($_POST["selObj"]);