topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Saturday December 14, 2024, 12:09 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
What is wrong with this php-snippet?
« on: December 08, 2010, 01: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 [Select]
  1. $vehicles=""; //line25
  2. if (isset($_POST["vehicle"])) //line26
  3. {$vehicles = "You chose: <br />"; //line27
  4. foreach ($_POST["vehicle"] as $value) //line28
  5. {$vehicles = $vehicles. $value . "<br />";} //line29
  6. print $vehicles;} //line230
« Last Edit: December 08, 2010, 02:01 AM by RedPillow »

JoTo

  • Super Honorary
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 236
    • View Profile
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #1 on: December 08, 2010, 02: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, 02:49 AM by JoTo »

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #2 on: December 08, 2010, 02: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 [Select]
  1. $vehicles="";
  2. if (isset($_POST["vehicle"]))
  3. {$vehicles = "You chose:<br />";
  4. $arrex = explode(" ", $_POST["vehicle"]);
  5. foreach($arrex as $value)
  6. {$vehicles = $vehicles . $value . "<br />";}
  7. print $vehicles;}
« Last Edit: December 08, 2010, 02:54 AM by RedPillow »

JoTo

  • Super Honorary
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 236
    • View Profile
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #3 on: December 08, 2010, 03: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, 03:16 AM by JoTo »

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #4 on: December 08, 2010, 03: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: HTML [Select]
  1. <form method=post action=''>
  2. <select name='color[]' size=4 multiple>
  3. <option value='blue'>Blue</option>
  4. <option value='green'>Green</option>
  5. <option value='red'>Red</option>
  6. <option value='yellow'>Yelllow</option>
  7. <option value='' selected>Select a Color </option>
  8. <option value='white'>White</option>
  9. <input type=submit></form>

///// collecting form data /////

Code: PHP [Select]
  1. @$color= $_POST['color'];
  2. if( is_array($color)){
  3. while (list ($key, $val) = each ($color)) {
  4. echo "$val <br>";
  5. }
  6. }//else{echo "not array";}


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

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #5 on: December 08, 2010, 03: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
    • View Profile
    • Donate to Member
Re: What is wrong with this php-snippet?
« Reply #6 on: December 08, 2010, 05:12 AM »
HiHo,

glad you solved it and you were welcome!

Greetings
JoTo