...

View Full Version : Invalid argument error in foreach()



MattB
03-17-2010, 03:50 PM
Hi Everyone,

Thanks for reading my post. I am new to PHP and wrote a script to process a simple volunteer form with checkboxes. Everything works fine if atleast one check box is checked. However, if they are all left unchecked, I get an invalid argument error. Here is the code I am using:




$to = "me@me.com";
$subject = "Volunteer Form";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email= $_POST['email'];
$comments = $_POST['comments'];

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$body = "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments\n$check_msg";


if (empty($email) || empty($firstname)) {
header( "Location: http://www.example.com/error.html" );}
else {

mail($to, $subject, $body);
header( "Location: http://www.example.com/thankyouvolunteer.html" );
}


I really appreciate any advice anyone has. I know this is probably really simple, but it has tortured me for 3 hours.

THANKS!

abduraooft
03-17-2010, 04:00 PM
Enclose all the above code inside an if statement, like

if(isset($_POST['firstname'])){
// all the code to process POST data goes here.

}

tomws
03-17-2010, 04:13 PM
Enclose all the above code inside an if statement, like

if(isset($_POST['firstname'])){
// all the code to process POST data goes here.

}

Shouldn't that be !empty() instead of isset()?

MattB
03-17-2010, 04:23 PM
Ok, I figured it out! Thanks so much Abduraooft!

Here is the revised code:



<?php
if(isset($_POST['check'])){

$to = "me@me.com";
$subject = "Volunteer Form";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email= $_POST['email'];
$comments = $_POST['comments'];

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$body = "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments\n$check_msg";


if (empty($email) || empty($firstname)) {
header( "Location: http://www.example.com/error.html" );}
else {

mail($to, $subject, $body);
header( "Location: http://www.example.com/thankyouvolunteer.html" );
}
}else{

$to = "me@me.com";
$subject = "Volunteer Form";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email= $_POST['email'];
$comments = $_POST['comments'];


$body = "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments";


if (empty($email) || empty($firstname)) {
header( "Location: http://www.example.com/error.html" );}
else {

mail($to, $subject, $body);
header( "Location: http://www.example.com/thankyouvolunteer.html" );
}
}
?>

Fou-Lu
03-17-2010, 04:27 PM
>.<
You're both looking at different things; abduraooft is pointing to ensure that the data is posted, while tomws is looking at validation.
You're actually always getting errors, some you see some you don't. The error is likely on the $check_msg since if nothing is checked the variable never exists, while if one is checked you're still getting an error indicating you're concatinating on an invalid variable. So its lose lose.
Initialize $checked_msg prior to the foreach loop as an empty string with $checked_msg = '';. Validate you're data as necessary as well, and wrap the entire block in a check to ensure that its been posted. There is no guarentee that any of this data has been passed, so you may want to add additional error checking to ensure it (though you have started with the check on empty for email and firstname). Using isset will determine if something exists, while using empty will check if it has content. You can get around isset calls by assigning by reference instead, but if you don't know what I mean when I say that don't try.


Also, the call against a header for a redirect still continues processing this script. This will not create errors in you're current code since its controlled with if/else combinations with no trailing processing, but you should consider adding an exit() call after you're headers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum