so I'm new here, and sorry if this question and/or code is formatted incorrectly. Basically I'm trying to echo a different statement depending on the checkboxes selected. If no checkboxes, I want to echo "You didn't select anything". If 2 checkboxes are selected, I want the word "and" in between the selections. If 3 or more, I want a comma after the first selections, and the word "and" between the last 2 selections. Any help would be great!
<form action="" method="post">
<p>Select your toppings!</p>
<input type="checkbox" name="toppings[]" value="Caramel" /> Caramel <br />
<input type="checkbox" name="toppings[]" value="Peanuts" /> Peanuts <br />
<input type="checkbox" name="toppings[]" value="Chocolate Syrup" /> Chocolate Syrup <br />
<input type="checkbox" name="toppings[]" value="Sprinkles" /> Sprinkles <br />
<input type="checkbox" name="toppings[]" value="Whipped Cream" /> Whipped Cream <br />
<input type="checkbox" name="toppings[]" value="Strawberries" /> Strawberries <br />
<input type="submit" name="button" value="Show Order"/>
</form>
<?php
$selected=$_POST['toppings'];
if(isset($_POST['button']))//run PHP on submit
{//show post data
echo "You ordered a Sundae with ";
//loop to store and display data
foreach($_POST['toppings'] as $selected){
echo " $selected";
}
}
?>
Aucun commentaire:
Enregistrer un commentaire