mmcg_33 Posted January 4, 2008 Share Posted January 4, 2008 I've just done a tutorial from a book (PHP and MySQL for Dynamic Websites - Larry Ullman). I'm pulling my hair out at the moment as I'm sure I've followed it to the letter, but can't get it to echo variables from an array of selection buttons. here's the code from the html page <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <form action="handle_multi_array.php" method="post"> <fieldset><legend>Enter your information in the form below:</legend> <p>Name: <input type="text" name="name" size="20" maxlength="40"/></p> <p>Interests: <input type="checkbox" name="interests[]" value="Music" /> Music <input type="checkbox" name="interests[]" value="Movies" /> Movies <input type="checkbox" name="interests[]" value="Books" /> Books <input type="checkbox" name="interests[]" value="Skiing" /> Skiing <input type="checkbox" name="interests[]" value="Napping" /> Napping </p> </fieldset> <input type="submit" name="submit" value="Submit my information" /> </form> </body> </html> and here's the php handler code... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php //VALIDATE THE NAME FIELD if(!empty($_POST['name'])){ $name = stripslashes($_POST['name']); } else{ $name = NULL; echo '<font color="red">You didn\'t enter your name!</font>'; } //VALIDATE THE CHECKBOXES if(isset($_POST['interests'])){ $interests = TRUE; } else{ $interests = NULL; echo '<p><font color="red">You didn\t select any interests!</font></p>'; } //CHECK SOMETHING WAS ENTERED FOR BOTH VARIABLES if($name && $interests){ echo "Thank you $name, you entered your interests as:<ul> "; foreach($_POST['$interests'] as $value) { echo "<li>$value</li>\n"; } echo '</ul>'; } else{ echo '<p><font color="red">Go back and fill out the form again</font></p>'; } ?> </body> </html> it's this bit I can't get to work - foreach($_POST['$interests'] as $value) { echo "<li>$value</li>\n"; } thanks Quote Link to comment Share on other sites More sharing options...
interpim Posted January 4, 2008 Share Posted January 4, 2008 Change your foreach to this and see if it helps foreach($_POST['$interests'] as $value) { echo '<li>$value</li>\n'; } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 4, 2008 Share Posted January 4, 2008 That wouldn't help. Try: foreach($_POST['interests'] as $value) { echo "<li>$value</li>\n"; } You shouldn't have had the $sign in there. interpim: FYI: text inside single quotes isn't parsed. If you want a variable to be echoed , you must place it inside double quotes, or no quotes at all. With that code, the text $value would literally be displayed. Quote Link to comment Share on other sites More sharing options...
interpim Posted January 4, 2008 Share Posted January 4, 2008 bah... i always get those 2 mixed up LOL Quote Link to comment Share on other sites More sharing options...
mmcg_33 Posted January 8, 2008 Author Share Posted January 8, 2008 yes it was the $ sign...doh! thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.