foreach + array + multiple checkboxes inside class [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

foreach + array + multiple checkboxes inside class [solved]

Post 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
Last edited by imstupid on Mon May 01, 2006 9:26 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're missing a semicolon.
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

parse errors generally indicate that you have something wrong with your code just before the thing it is referencing, not after.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post 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.
Post Reply