panthers_uni Posted December 1, 2011 Share Posted December 1, 2011 Hey forum, I am a new php user and am having some issues (probably pretty basic to the likes of more experienced users), I am trying to make a script that has 4 checkboxes and a submit button, the values of the checkboxes should be passed through the array using a foreach loop, I got the basic template setup but when trying to pass the array I ran into a few problems. If anyone would take a peek at my code and point me in the right direction it would be much appreciated. Thanks forum. <html> <title>Toppings</title> <body> <p>What do you want on your pizza?</p> <form action="ProcessToppings.php" method="post"> <?php //define varialbes $Output_form = false; $submit = $_POST['submit']; if(isset($submit)) { if(empty($toppings)) { echo '<b>You forgot to choose a topping(s).</b>'; echo '<br>'; echo '<br>'; $Output_form = true; } else { $toppings=array("0", "1", "2", "3"); foreach ($toppings ['toppings'] as $order) { echo 'Your pizza will have these topping: ' . $toppings . '<br/>'; $Output_form = false; } } } ?> <input type="checkbox" name="toppings[0]" value="pepperoni" />Pepperoni<br /> <input type="checkbox" name="toppings[1]" value="sausage" />Sausage<br /> <input type="checkbox" name="toppings[2]" value="mushrooms" />Mushrooms<br /> <input type="checkbox" name="toppings[3]" value="olives" />Olives <p> <input type="submit" value="Place Order" name="submit" /></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/ Share on other sites More sharing options...
marcus Posted December 1, 2011 Share Posted December 1, 2011 $toppings = $_POST['toppings']; foreach($toppings AS $key => $value){ echo "Your pizza will have " . htmlentities($value) . " as a topping.<br/>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/#findComment-1293252 Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 There are a few other problems too. Toppings What do you want on your pizza? $Output_form = true; // the default action is to show the form if (isset($_POST["submit"])) { // you have to use isset() right on the $_GET or $_POST array if (empty($_POST["toppings"])) { // same for empty() echo " You forgot to choose a topping(s). "; // strong tags are better than b tags } else { // I'm guessing you want a list like "pepperoni, sausage, mushrooms" echo " Your pizza will have these toppings: "; $toppings = implode(", ", (array)$_POST["toppings"]); // use typecasting to prevent one kind of form modification attack echo htmlentities($toppings); // since the values come from $_POST use htmlentities() to prevent XSS attacks echo ""; $Output_form = false; } } if ($Output_form) { ?> </pre> <form action="ProcessToppings.php" method="post"> Pepperoni Sausage Mushrooms Olives </form> <br><b Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/#findComment-1293255 Share on other sites More sharing options...
panthers_uni Posted December 1, 2011 Author Share Posted December 1, 2011 What is the htmletities? I have never used that before same with the implode function? Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/#findComment-1293258 Share on other sites More sharing options...
panthers_uni Posted December 1, 2011 Author Share Posted December 1, 2011 And why are you guys so heavy on the double quotes? I have been taught to use single for the most part. Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/#findComment-1293259 Share on other sites More sharing options...
requinix Posted December 1, 2011 Share Posted December 1, 2011 What is the htmletities? I have never used that before same with the implode function? Check the PHP manual. htmlentities implode And why are you guys so heavy on the double quotes? I have been taught to use single for the most part. Then keep using single quotes. I, for one, prefer double quotes. Possibly because I'm used to C#, possibly because my strings often contain variables. Quote Link to comment https://forums.phpfreaks.com/topic/252252-foreach-loops-and-checkboxes/#findComment-1293273 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.