Click to See Complete Forum and Search --> : can someone please tell me whats wrong with this


prognoob
January 5th, 2005, 10:07 PM
<html>
<head>
<title>an html form including a select element</title>
</head>
<body>
<form action = "eg9.5.php" method = "POST">
<input type = "text" name = "user">
<br>

<textarea name ="address" rows = "5" cols ="40">
</textarea>
<br>

<select name = "products[]" multiple>
<option>sonic screwdriver
<option>tricoders
<option>ORAC AI
<option>HAL 2000
</select>
<br>
<input type = "submit" value ="click here to submit">

</form>
</body>
</html>


and the script


<html>
<head>
<title> listing 9.5 reading input from a form in listing 9.4</title>
</head>
<body>
<?
echo "welcome:".($_POST['user']);
echo "<br>your address is:".($_POST['address']);
echo "<br>Your products choices are:";

foreach($products as $value)
{
echo "$value";
}
?>
</body>
</html>


this is the error i get when i fill out the above form and click submit.

"welcome: pedro
your address is: california beverly hills 90210
Your products choices are:
error: Warning: Invalid argument supplied for foreach() in c:\program files\apache group\apache\htdocs\eg9.5.php on line 11"

i've spent about five hours just on that and still can not figure out whats wrong with it .

visualAd
January 6th, 2005, 02:37 AM
Like your other varaibles passed from your form; the products array will be inside the $_POST variable. You access it using the same convention yoou used to access the other ones $_POST['products'].

Nibinaear
January 23rd, 2005, 06:28 AM
In your html code you named the products element products[] as though it were an array. You should have called the element, "products" for simplicity, then taken it from the form like:

echo "<br>Your products choices are:".$_POST['products'];



Also, it might make things easier to put each element in a variable like:

$name = $_POST['user'];

echo "welcome:".$name;

visualAd
January 23rd, 2005, 06:48 AM
In your html code you named the products element products[] as though it were an array. You should have called the element, "products" for simplicity, then taken it from the form like:

echo "<br>Your products choices are:".$_POST['products'];



Also, it might make things easier to put each element in a variable like:

$name = $_POST['user'];

echo "welcome:".$name;
Nibinaear: The reason he named the form element products[] is becuase he wants it to be an array as he is using a dropdown list that allows multiple selections. Naming it products will only load the first selection into the variable and effectively negitate the point of allowing multple selections.

Nibinaear
January 23rd, 2005, 02:07 PM
name = products is html and not php, I assumed that would not work.

Sander@the-new-world.com
April 18th, 2005, 03:44 PM
The code you wrote should be the following:

echo "<br>Your products choices are:";

foreach($_POST['products'] as $value)
{
echo $value;
}

This should work because I use a similar code in website I develop.

Kind regards,

Sander

http://www.the-new-world.com (http://www.the-new-world.com/)
The #1 Webdevelopment and Webhosting Portal