Page 1 of 1

foreach + array + multiple checkboxes inside class [solved]

Posted: Fri Apr 28, 2006 2:15 pm
by imstupid
Hey Everybody, it's been a while and I'm a little rusty.

So I've got a form with multiple checkboxes, with the name value assigned as name="food[]"

Following the checkbox tutorial, I was able to split it up into the foreach section as suggested.

The only problem, is I'm using a php mail class, and am having trouple displaying the array into the body of the email properly.

Example of html form code:

Code: Select all

// blah blah

<input type="checkbox" name="food[]" value="meatballs" />

// blah blah

And for the php/mail class:

Code: Select all

$mail->Body = "hello, " foreach ($_POST['food'] as $food){ echo $food." is what you selected for your favorite food";}
And my error message is
Parse error: parse error, unexpected T_FOREACH in....

So I know I'm doing something wrong with the syntax in that line, (It's got to be that curly bracket), perhaps something with echoing? As always, any advice/hints/help is much appreciated. Hope everyone is well.

j

Posted: Fri Apr 28, 2006 4:02 pm
by feyd
You're missing a semicolon.

Posted: Fri Apr 28, 2006 4:33 pm
by imstupid
thanks for the reply, I tried:

Code: Select all

$mail->Body = "hello, " foreach ($_POST['food'] as $food){  echo $food."is what you selected for your favorite food.";};
and it's still busting out the usual "parse error, unexpected T_FOREACH" error. That looks right, but it can't be... I'm losing the touch I never had.

Posted: Fri Apr 28, 2006 4:36 pm
by feyd
parse errors generally indicate that you have something wrong with your code just before the thing it is referencing, not after.

Posted: Fri Apr 28, 2006 5:03 pm
by John Cartwright

Code: Select all

$mail->Body = "hello, " 

foreach ($_POST['food'] as $food){ 
   echo $food." is what you selected for your favorite food";
}
by making your code more readable, it is easier to spot errors ;)

Posted: Mon May 01, 2006 9:25 am
by imstupid
Thanks guys-
just so the others who are "less than perfect" at php, I was trying to do this more or less:

Code: Select all

$mail->Body = "hello, "; 

foreach ($_POST['food'] as $food)
{
$mail->Body .= $food."\n";  
}
$mail->Body .= "is what you selected as your favorite food(s)";
and since I always forget about the period before the equal sign, nothing was working out to well.