<?php
setcookie("custName", $_POST['custName']);
setcookie("pizzaSize", $_POST['pizzaSize']);
setcookie("pizzaToppings", serialize($_POST['pizzaToppings']));
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Order Some Pizza</title</head>
<body>

<h1>Welcome to Joe's Pizza!</h1>

<?php

if ($_POST['pizzaStatus'] == "Place Order") {

   $_POST['custName'] = htmlentities($_POST['custName']);
   
   if (strlen($_POST['custName']) < 1) {
      $errorMessages[] = "Please enter your Name.";
   }
   
   if(!ctype_alpha($_POST['pizzaSize'])) {
      $errorMessages[] = "Please choose a Size.";
   }
      
   if (is_array($_POST['pizzaToppings'])) {
      if(!ctype_alpha(str_replace(" ","a", implode("a",$_POST['pizzaToppings'])))) {
         $errorMessages[] = "Please choose some Toppings.";
      }
   }

   if(is_array($errorMessages)) {
      echo "<ul>";
      foreach($errorMessages as $message) {
         echo "<li>$message</li>";
      }
      echo "</ul>";
      displayForm($_POST['custName'], $_POST['pizzaSize'], $_POST['pizzaToppings']);
   }
   else {
      echo "<a href=\"printReceipt.php\">Printable Receipt</a>";
      echo "<p>This order is for {$_POST['custName']}</p>
      <p>Size: ".$_POST['pizzaSize']."</p>
      <p>Toppings:</p><ul>";
      foreach($_POST['pizzaToppings'] as $topping)  {
         echo "<li>$topping</li>";
      }
      echo "</ul>";
   }
}
else {
   displayForm($_POST['custName'], $_POST['pizzaSize'], $_POST['pizzaToppings']);
}


function displayForm($custName, $pizzaSize, $pizzaToppings) {
   echo "<form action=\"./orderPizza.php\" method=\"POST\">
   <p>Name: <input type=\"text\" name=\"custName\" maxlength=\"200\" value=\"$custName\" /></p>
   <p>Choose a Size:<br />
   <input type=\"radio\" name=\"pizzaSize\" value=\"Small\" ";
   if($pizzaSize == "Small") { echo "checked "; }
   echo "/> Small<br />
   <input type=\"radio\" name=\"pizzaSize\" value=\"Medium\" ";
   if($pizzaSize == "Medium") { echo "checked "; }
   echo "/> Medium<br />
   <input type=\"radio\" name=\"pizzaSize\" value=\"Large\" ";
   if($pizzaSize == "Large") { echo "checked "; }
   echo "/> Large</p>
   <p>Add Additional Toppings:<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Mushrooms\" ";
   if(is_array($pizzaToppings) && in_array("Mushrooms",$pizzaToppings)) { echo "checked "; }
   echo "/> Mushrooms<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Green Peppers\" ";
   if(is_array($pizzaToppings) && in_array("Green Peppers",$pizzaToppings)) { echo "checked "; }
   echo "/> Green Peppers<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Black Olives\" ";
   if(is_array($pizzaToppings) && in_array("Black Olives",$pizzaToppings)) { echo "checked "; }
   echo "/> Black Olives<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Extra Cheese\" ";
   if(is_array($pizzaToppings) && in_array("Extra Cheese",$pizzaToppings)) { echo "checked "; }
   echo "/> Extra Cheese<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Pepperoni\" ";
   if(is_array($pizzaToppings) && in_array("Pepperoni",$pizzaToppings)) { echo "checked "; }
   echo "/> Pepperoni<br />
   <input type=\"checkbox\" name=\"pizzaToppings[]\" value=\"Sausage\" ";
   if(is_array($pizzaToppings) && in_array("Sausage",$pizzaToppings)) { echo "checked "; }
   echo "/> Sausage</p>
   <input type=\"submit\" name=\"pizzaStatus\" value=\"Place Order\" />
   </form>";
}

?>

</body></html>