panthers_uni Posted December 2, 2011 Share Posted December 2, 2011 Can anyone help me complete this script I can get it working with using an implode function, but for the purpose of knowledge I want to not use it. I just want it to echo back the corresponding checkboxes, but I can't get the echoing correct, it echoes back array not the value? <html> <title>What do you want on your Tombstone?</title> <body> <?php $Output_form = true; if (isset($_POST['submit'])) { if (empty($_POST['toppings'])) { echo '<b>You forgot the toppings for your Tombstone.</b>'; echo '<br>'; echo '<br>'; } else { echo '<p><b>This is what you are getting on your Tombstone: '; (array)$_POST['toppings']; echo $toppings; echo '!'; echo '</p></b>'; $Output_form = false; } } if ($Output_form) { ?> <form action="ProcessToppings.php" method="post"> <p><i><h2>What do you want on your Tombstone?</h2></i></p> <p> <input type="checkbox" name="toppings[]" value="pepperoni" /> Pepperoni<br /> <input type="checkbox" name="toppings[]" value="sausage" /> Sausage<br /> <input type="checkbox" name="toppings[]" value="mushrooms" /> Mushrooms<br /> <input type="checkbox" name="toppings[]" value="cheese" /> Cheese<br /> <input type="checkbox" name="toppings[]" value="olives" /> Olives</p> <p> <input type="submit" value="Make that Peet-Za!" name="submit" /></p> </form> <?php } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/252271-complete-a-foreach-array-without-a-implode-function/ Share on other sites More sharing options...
Psycho Posted December 2, 2011 Share Posted December 2, 2011 Use foreach() to iterate over each element in the array echo '<p><b>This is what you are getting on your Tombstone: '; foreach($_POST['toppings'] as $topping) { echo " - $topping<br>\n"; } echo '</p></b>'; Link to comment https://forums.phpfreaks.com/topic/252271-complete-a-foreach-array-without-a-implode-function/#findComment-1293332 Share on other sites More sharing options...
KevinM1 Posted December 2, 2011 Share Posted December 2, 2011 Since you're trying to access a $toppings variable that wasn't assigned to, or otherwise initialized, it looks like you have register_globals on, which is a very big security risk. In fact, register_globals is turned off by default in PHP5+ and only exists for backwards compatibility. I also believe that register_globals will be removed from PHP altogether relatively soon. Also, you don't need to cast $_POST['toppings'] as an array. PHP already turned it into an array because you named each check box toppings[] in your HTML form. All that said, what you want to do is fairly trivial: $implodedToppings = ''; foreach($_POST['toppings'] as $value) { $implodedToppings .= '$value, '; } $implodedToppings = substr($implodedToppings, 0, -2); // get rid of the extra comma and space at the end echo $implodedToppings; Link to comment https://forums.phpfreaks.com/topic/252271-complete-a-foreach-array-without-a-implode-function/#findComment-1293335 Share on other sites More sharing options...
panthers_uni Posted December 2, 2011 Author Share Posted December 2, 2011 Awesome thanks for the help fellas. Link to comment https://forums.phpfreaks.com/topic/252271-complete-a-foreach-array-without-a-implode-function/#findComment-1293599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.