<html>
<head>
<title>Order Ice Cream</title>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>
<h2>Process using same page. Did you lose something?</h2>
<?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE); //all but warnings & notices
$firstname = $_POST['firstname']; #get HTML form entry fields
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$flavor = $_POST['flavor']; #select list --> array
$topping = $_POST['topping']; #checkboxes --> array
$creditCard = $_POST['creditCard'];
$msg; #create an empty msg variable
if ($_POST) #if data was collected in _POST array
{
validate();
}
display();
//=============================================================================
function validate()
{
global $firstname, $lastname, $address, $flavor, $topping, $creditCard;
global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;
if (is_array($flavor))
$flavor = implode(',' , $flavor); #flatten out the array
if (is_array($topping))
$topping = implode(',' , $topping); #flatten out the array
if ($firstname == '' or $lastname == '') {
$msg = 'error';
$name_error = '*';
}
if ($address == '') {
$msg = 'error';
$addr_error = '*';
}
if ($flavor == '') {
$msg = 'error';
$flav_error = '*';
}
if ($topping == '') {
$msg = 'error';
$top_error = '*';
}
if ($creditCard[0] == '') {
$msg = 'error';
$cc_error = '*';
}
if ($msg == 'error')
$msg = 'Please enter required field(s) above!';
else
{
$msg = "$firstname $lastname at $address <br/>";
$msg .= "ordered $flavor with $topping <br/>";
$msg .= "using credit card $creditCard <br/>";
}
}
//=============================================================================
function display()
{
global $firstname,$lastname,$address;
global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;
print "<form method=POST action=$_SERVER[PHP_SELF] >"; //using $_SERVER[PHP_SELF] instead of form4.php
print "<fieldset style='width:570px; height:310px; border-color:gold'>"; //to make it work in PHP*Tester
print "<legend>Enter Fields Below</legend>";
print "<table bgcolor=eeeeee > \n";
print "<tr>";
print "<td><b>First Name ";
print "<td><input type=text name=firstname value=$firstname > \n";
print "<tr>";
print "<td><b>Last Name <font color=red> $name_error </font>";
print "<td><input type=text name=lastname value=$lastname > \n";
print "<tr>";
print "<td><b>Enter Address <font color=red> $addr_error </font>";
print "<td><textarea name=address rows=4 cols=47>$address</textarea> \n";
print "<tr>";
print "<td><b>Ice Cream Flavor <font color=red> $flav_error </font> \n";
print "<td><select name='flavor[]' SIZE='4' multiple='multiple'> \n";
print " <option value='vanilla' > Vanilla</option> \n";
print " <option value='chocolate' > Chocolate</option> \n";
print " <option value='strawberry' > Strawberry</option> \n";
print " <option value='butter-pecan' > Butter Pecan</option> \n";
print " <option value='rocky-road' > Rocky Road</option> \n";
print " <option value='french-vanilla'> French Vanilla</option> \n";
print " <option value='pistachio' > Pistachio</option> \n";
print "</select> \n";
print "<tr>";
print "<td><b>Select Topping <font color=red> $top_error </font> \n";
print "<td>";
print "<input type='checkbox' name='topping[]' value='hotFudge' /> Hot Fudge \n";
print "<input type='checkbox' name='topping[]' value='sprinkles' /> Sprinkles \n";
print "<input type='checkbox' name='topping[]' value='nuts' /> Nuts \n";
print "<input type='checkbox' name='topping[]' value='whippedCream'/> Whipped Cream \n";
print "<tr>";
print "<td><b>Choose Credit Card <font color=red> $cc_error </font> \n";
print "<td>";
print "<input type=radio name=creditCard value='visa' /> Visa \n";
print "<input type=radio name=creditCard value='master-card'/> Master Card \n";
print "<input type=radio name=creditCard value='amex' /> Amex \n";
print "<tr>";
print "<td width=155>";
print "<input type=submit value=' Place Order '>";
print "<td><input type=reset value=Cancel> \n";
print "</table> \n";
print "</fieldset> \n";
print "<br><font color=red> $msg </font> \n";
print "<br/>";
print "</form> \n";
}
?>
<?php include "../include.php"; ?> <!-- hyperlink to see the code -->
</body>
</html>