ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

What is wrong with this php-snippet?

(1/2) > >>

RedPillow:
Here, it comes up with this error "Invalid argument supplied for foreach() in J:\Xampp\xampp\htdocs\forms-1\next.php  on line 28
I read somewhere that the variable it's checking isn't array ... it gets the $_POST vehicle from listbox with multiple selections.


--- Code: PHP ---$vehicles=""; //line25if (isset($_POST["vehicle"])) //line26{$vehicles = "You chose: <br />"; //line27foreach ($_POST["vehicle"] as $value) //line28{$vehicles = $vehicles. $value . "<br />";} //line29print $vehicles;} //line230

JoTo:
Hi there,

i think the problem is, that foreach needs an array. While $_POST is indeed an array, $_POST["vehicle"] is NOT. It's normally a scalar, unless its a group of controls (e.g. checkboxes).

Either you use $_POST["vehicle"] without any loop (e.g. in that variable is contained "vehicleA vehicleB vehicleC" you'll get what you want without a loop but without the line breaks), or you do an explode before the loop like this (assuming the vehicles in that variable are divided by a space):

$arrTmp = explode(" ", $_POST["vehicle"]);
foreach($arrTmp as $value)
{
  // do what you want now with $value
}

You can test if $_POST["vehicle"] is an array or not with the function is_array(). If it's the result from e.g. checkboxes, it should work as you did. Then i'm out of ideas, sorry.

HTH
JoTo

RedPillow:
Thanks, your explode-function works!
Though there is still a tiny problem..
With this, it only prints the last selected option in the listbox ie.

Option 1 -selected-
Option 2
Option 3 -selected-

output --> Only option 3

Btw, the value $_POST gets is not an array ... and it should be I guess since you can choose multiple options from the listbox.


--- Code: PHP ---$vehicles="";if (isset($_POST["vehicle"])){$vehicles = "You chose:<br />";$arrex = explode(" ", $_POST["vehicle"]);foreach($arrex as $value){$vehicles = $vehicles . $value . "<br />";}print $vehicles;}

JoTo:
Hi again,

A LISTBOX, aha. I am not sure right out of the head how the single strings in a submitted listbox content are divided. Is it \n, \r, \r\n, < br />?

I think the problem is, that you need another split character in your explode (first parameter where i used the space character in my example - " ").

See first, whats exactly in your $_POST["vehicle"] variable contained (e.g. write the original $_POST variable to a file and use a hex viewer) and use the correct split character in explode.

Otherwise, what element do you use exactly in your html page? If you want, you can use a < select> element, which should deliver back a proper array with the selected items, ready to use with foreach without any exploding before.

For a sample look here:
http://www.onlinetools.org/tricks/using_multiple_select.php

HTH
JoTo

RedPillow:
Writing to file didn't work so well ... it only wrote the last option selected in the listbox to the file.

But here's a code from some php-site they used for multiple selections:


--- Code: HTML ---<form method=post action=''><select name='color[]' size=4 multiple><option value='blue'>Blue</option><option value='green'>Green</option><option value='red'>Red</option><option value='yellow'>Yelllow</option><option value='' selected>Select a Color </option><option value='white'>White</option></select><input type=submit></form>
///// collecting form data /////


--- Code: PHP ---@$color= $_POST['color'];if( is_array($color)){while (list ($key, $val) = each ($color)) {echo "$val <br>";}}//else{echo "not array";}

I fail to see what this snippet does differently ... maybe you can?

Navigation

[0] Message Index

[#] Next page

Go to full version