Micaela's Create Your Milkshake!
CODE FOLLOWS
<?php
include('../includes/header.php');
// code
error_reporting(E_ALL);
echo('<h1>Micaela\'s Create Your Milkshake!</h1>');
// Note the variables being passed via the POST ternary operators.
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$milkshake_name = isset($_POST['milkshake_name']) ? $_POST['milkshake_name'] : '';
$milk_type = isset($_POST['milk_type']) ? $_POST['milk_type'] : '';
$topping1 = isset($_POST['topping1']) ? $_POST['topping1'] : '';
$topping2 = isset($_POST['topping2']) ? $_POST['topping2'] : '';
// fruits array checkbox
$fruits = array();
if (isset($_POST['fruits']))
{
foreach ($_POST['fruits'] as $value)
{
$fruits[] = htmlentities($value);
}
}
$milkshake_name = htmlentities($milkshake_name);
$milk_type = htmlentities($milk_type);
$topping1 = htmlentities($topping1);
$topping2 = htmlentities($topping2);
$c = 'CHECKED';
$s = 'SELECTED';
if ($step == 0) // first time into this form
{
?>
<form action="milkshake.php" method="POST" class="pad">
<fieldset>
<legend>Milkshake Name</legend>
<input type="text" name="milkshake_name" size="36"
value="<?php echo($milkshake_name); ?>">
</fieldset>
<br>
<fieldset>
<legend>Milk Type</legend>
<input type="radio" name="milk_type" value="Whole Milk"
<?php if ($milk_type == 'Whole Milk') echo($c); ?> >
Whole Milk<br>
<input type="radio" name="milk_type" value="Oat Milk"
<?php if ($milk_type == 'Oat Milk') echo($c); ?> >
Oat Milk<br>
</fieldset>
<br>
<fieldset>
<legend>Fruits (Pick as many as you like!!!)</legend>
<?php
$fruit_options = array("Banana","Strawberry","Watermelon","Mango","Pineapple"); // fruits options array
// Loop though fruits and generates checkboxes
foreach ($fruit_options as $fruit)
{
echo('<input type="checkbox" name="fruits[]" value="'.$fruit.'" ');
if (in_array($fruit, $fruits)) echo($c);
echo('> '.$fruit.'<br>');
}
?>
</fieldset>
<br>
<fieldset>
<legend>Toppings</legend>
<select name="topping1">
<option value="">Choose Topping 1</option>
<option value="Vanilla Syrup" <?php if ($topping1 == 'Vanilla Syrup') echo($s); ?>>Vanilla Syrup</option>
<option value="Chocolate Syrup" <?php if ($topping1 == 'Chocolate Syrup') echo($s); ?>>Chocolate Syrup</option>
<option value="Whipped Cream" <?php if ($topping1 == 'Whipped Cream') echo($s); ?>>Whipped Cream</option>
</select>
<br><br>
<select name="topping2">
<option value="">Choose Topping 2</option>
<option value="Strawberry" <?php if ($topping2 == 'Strawberry') echo($s); ?>>Strawberry</option>
<option value="Cherry" <?php if ($topping2 == 'Cherry') echo($s); ?>>Cherry</option>
<option value="Sprinkles" <?php if ($topping2 == 'Sprinkles') echo($s); ?>>Sprinkles</option>
</select>
</fieldset>
<br>
<input type="hidden" name="step" value="1">
<input type="submit" value="Submit">
</form>
<?php
}
else // else show what the form gathered
{
?>
<h2>Your Milkshake Creation</h2>
<p><strong>Name:</strong> <?php echo($milkshake_name); ?></p>
<p><strong>Milk:</strong> <?php echo($milk_type); ?></p>
<p><strong>Fruits:</strong> <?php echo(implode(', ', $fruits)); ?></p>
<p><strong>Toppings:</strong> <?php echo($topping1 . ', ' . $topping2); ?></p>
<br>
<img src="../images/milkshake-icecream.gif" width="250">
<br><br>
<form action="milkshake.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" value="Try Again">
</form>
<?php
}
include('../includes/footer.php');
?>