DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: What is wrong with this php-snippet?  (Read 7,393 times)

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
What is wrong with this php-snippet?
« on: December 08, 2010, 07:51 AM »
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=""; //line25
if (isset($_POST["vehicle"])) //line26
{$vehicles = "You chose: <br />"; //line27
foreach ($_POST["vehicle"] as $value) //line28
{$vehicles = $vehicles. $value . "<br />";} //line29
print $vehicles;} //line230
« Last Edit: December 08, 2010, 08:01 AM by RedPillow »

JoTo

  • Super Honorary
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 236
    • Gathering Badge
    • Participated in NANY Challenge Twice
Re: What is wrong with this php-snippet?
« Reply #1 on: December 08, 2010, 08:38 AM »
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
« Last Edit: December 08, 2010, 08:49 AM by JoTo »

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: What is wrong with this php-snippet?
« Reply #2 on: December 08, 2010, 08:49 AM »
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;}
« Last Edit: December 08, 2010, 08:54 AM by RedPillow »

JoTo

  • Super Honorary
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 236
    • Gathering Badge
    • Participated in NANY Challenge Twice
Re: What is wrong with this php-snippet?
« Reply #3 on: December 08, 2010, 09:04 AM »
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
« Last Edit: December 08, 2010, 09:16 AM by JoTo »

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: What is wrong with this php-snippet?
« Reply #4 on: December 08, 2010, 09:17 AM »
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: (html4strict)
<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?

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: What is wrong with this php-snippet?
« Reply #5 on: December 08, 2010, 09:28 AM »
_______
SOLVED

My html-side was messed-up really bad ...
I didn't even have a value for each option.

-- But hey, thanks a lot for your time and help !!

JoTo

  • Super Honorary
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 236
    • Gathering Badge
    • Participated in NANY Challenge Twice
Re: What is wrong with this php-snippet?
« Reply #6 on: December 08, 2010, 11:12 AM »
HiHo,

glad you solved it and you were welcome!

Greetings
JoTo

Original address: https://www.donationcoder.com/forum/index.php?topic=24817.0