1

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";

  }

  }
?>
Evan Smyth
  • 11
  • 2
  • 2
    Make use of [count()](http://php.net/manual/en/function.count.php) with a conditional. – Funk Forty Niner Jan 22 '15 at 01:19
  • 3
    I believe I have an old answer for that here: http://stackoverflow.com/questions/7687665/replace-the-last-comma-with-an-sign/7687699#7687699 You can use your array `$_POST['toppings']` directly in place of the `$bedroom_array` in this answer. – Michael Berkowski Jan 22 '15 at 01:24

0 Answers0