Jump to content

Foreach Loops and Checkboxes


panthers_uni

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.