Jump to content

Invalid argument supplied for foreach()


real_illusions

Recommended Posts

I get this error when i process a form -Invalid argument supplied for foreach() in (path to file) on line 9The email is still sent, but its blank.The line in question isforeach ($_POST['interest'] as $index => $value) {changing $value to $interest doesn't make any difference.All the checkboxes (that this line is supposed to check) have "interest" as the name value.What could be the problem?

Link to comment
Share on other sites

$_POST is an array, but $_POST['interest'] (i.e. the "interest" member of the array) is not. It's most probably a string. And foreach is only for arrays. You could do

foreach ($_POST as $index => $value) {

In which case when you reach the 'interest' field,

$index = 'interest';$value = 'the value from the POST field';

Link to comment
Share on other sites

Now you're iterating through $_POST instead of through the array of checkboxes. Make sure to only include the brackets on the name for things that you want to store as an array. Post the rest of the loop also.
huh? can you repeat that in english? :)heres lines 9,10 and 11foreach ($_POST as $index => $value) { $message .= "$index [$interest]" . "\n";}
Link to comment
Share on other sites

OK. Look at what that loop does. If you have these form elements:

<input type="text" name="text1"><input type="checkbox" name="check1[]" value="1"><input type="checkbox" name="check1[]" value="2"><input type="checkbox" name="check1[]" value="3"><select name="select1">  <option value="1">1</option>  <option value="2">2</option>  <option value="3">3</option></select>

Then the foreach loop you posted above will print this:text1 [$interest]check1 [$interest]select1 [$interest]whatever $interest is it will fill in the value there. So that loop is telling it to print the names of the form elements, plus the $interest variable in square brackets.

Link to comment
Share on other sites

so thatforeach ($_POST as $index => $value) {$message .= "$index [$interest]" . "\n";}should beforeach ($_POST as $index => $value) {$message .= "interest [$interest]" . "\n";}as my checkboxes are<input type="checkbox" name="interest[]" value="1"><input type="checkbox" name="interest[]" value="2"><input type="checkbox" name="interest[]" value="3">Is that still completely wrong? As the email is still blank when i do that. Again no errors though.Not sure where the $interest variable comes from though, especially in your example as you dont use it...

Link to comment
Share on other sites

foreach($_POST['interests'] as $value){ echo $value."<br />";}

Try that... change interests to interest depending on the checkbox names

Link to comment
Share on other sites

thanks all. Solved the one problem only to encounter another.Spotted the mistake that caused nothing to be sent. The checkboxes work now.Now i get some bits of info, but not all. I have the following, it gets sent, but the variables that its supposed to read from the form dont get thru, apart from name (and checkboxes). I copied the name line for the other variables, but it didn't work..$subject = "Contact form test";$message .= "Name: " . $_POST['name'] . "\n";$message .= "Contact Telephone (day): " . $_POST['daytimephone'] . "\n";$message .= "Contact Main: " . $_POST['phonenumber'] . "\n";$message .= "Address:" . "\n";$message .= "Line 1: " . $_POST['address'] . "\n"; $message .= "Postcode: " . $_POST['postcode'] . "\n"; $message .= "Development: " . $_POST['developmentinterested'] . "\n"; $message .= "How they heard about us: " . $_POST['howfindwebsite'] . "\n"; $message .= "Comments: " . $_POST['message'] . "\n"; foreach($_POST['interest'] as $value){$message .= "$value" . "\n";}$message .= "---- End of Comments ----" . "\n";The variables aren't in " like the other bits that do get printed in the email, but then the name isn't in " but that gets sent. I've tried splitting up the lines, like$message .= $_POST['address'] . "\n";$message .= $_POST['postcode'] . "\n";but that doesn't work either..

Link to comment
Share on other sites

i stuck that line after the "end of comments", but nothing appeared in the email, and nothing came up on screen apart from the "thank you" message i have when its sent succesfully.
Try to place it at the very top, rather then the bottom.
Link to comment
Share on other sites

You have to see something. If you aren't seeing anything then something is not right. Since $_POST is a superglobal at the very least you should see this:Array()Even if there is nothing in it. The print_r statement can go anywhere on the page that processes the form. Make sure you don't put it in an if statement or something that's not going to get executed.

Link to comment
Share on other sites

i guess something isn't right then...this is the full script...

<?phpif(isset($_POST['submit'])) {$to = "emailgoeshere";$subject = "Contact form test";$message .= "Name: " . $_POST['name'] . "\n";$message .= "Contact Telephone (day): " . $_POST['daytimephone'] . "\n";$message .= "Contact Main: " . $_POST['phonenumber'] . "\n";$message .= "Address:" . "\n";$message .= "Line 1: " . $_POST['address'] . "\n"; $message .= "Postcode: " . $_POST['postcode'] . "\n"; $message .= "Development: " . $_POST['developmentinterested'] . "\n"; $message .= "How they heard about us: " . $_POST['howfindwebsite'] . "\n"; $message .= "Comments: " . $_POST['message'] . "\n"; foreach($_POST['interest'] as $value){$message .= "$value" . "\n";}$message .= "---- End of Comments ----" . "\n"; echo "Thank you for your email. One of our representatives will be in contact shortly.<br /><a href=\"index.htm\">Click here</a> to return to the homepage.";mail($to, $subject, $message);} else {echo "Error - please <a href=\"index.htm\">click here</a> to return to the homepage.";}?>

Link to comment
Share on other sites

Are you sure you're updating the file, or running the right thing? Add an echo statement to make sure you're running what you think you are. If you submit a form to a page that has print_r($_POST) on it you *will* see something. Also, make sure the form is using post and not get. And make sure that output buffering is not enabled. You can also try this instead of print_r:

echo "\$_POST: {<br>";foreach ($_POST as $k => $v){  echo "\$_POST[{$k}] = \"{$v}\"<br>";}echo "}<br>";

If you want to see a whole lot of information, you can also try print_r($GLOBALS).

Link to comment
Share on other sites

  • 2 weeks later...

sorry for the looooooooong delay in replying. But finally got round to attempting this again.

echo "\$_POST: {<br>";foreach ($_POST as $k => $v){  echo "\$_POST[{$k}] = \"{$v}\"<br>";}echo "}<br>";

i tried that..it seemed to work.Although the interest bit, with the checkboxes, just returned "Array".but yet the email is blank...
Link to comment
Share on other sites

All fixed now..the email now sends fine and has content in it.However, it doesn't work on the websites server..it says it sent..but nothing appears. Not sure what could be up there but i dont think i can do anything about it...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...