I have finally almost figured out my coding project after many rewrites, but now I can’t get the errors to appear although I had them working before adding the submit-button IF statements to solve another problem. Now I can’t figure out how to get them to populate without causing both the form and the thank you message on the same page.
The other problem that I am having is that the input from the customer in the form or dropdowns is not sticking if the page is redirected to have them fix an issue (error).
Here is my lengthy code (this is all supposed to be in PHP and reprocess on one page for my assignment). I know I shouldn’t need to put the variables at the top, but without it, I was getting a lot of warnings and this was the only way that I could find to solve it. My teacher also recommends that we do this so I would prefer to leave them at this time.
<?php include_once ('includes/nav.php');?> <!--NAVIGATION-->
<?php
//VARIABLES
$firstName= $lastName = $email = $phone = $address= $city = $state = $zip = "";
$band = $bands = $color = $colors = $size = $sizes = $style = $styles = "";
$error_ = $error = $message = $email_from = "";
if(empty($_POST['submit-button'])) //BUTTON HAS NOT BEEN PUSHED
{ //CHECK FOR GET VARIABLES
if(!empty($_GET['firstName'])) $firstName = $_GET['firstName']; else $firstName = "";
if(!empty($_GET['lastName'])) $lastName = $_GET['lastName']; else $lastName = "";
if(!empty($_GET['email'])) $email = $_GET['email']; else $email = "";
if(!empty($_GET['phone'])) $phone = $_GET['phone']; else $phone = "";
if(!empty($_GET['address'])) $address = $_GET['address']; else $address = "";
if(!empty($_GET['city'])) $city = $_GET['city']; else $city = "";
if(!empty($_GET['state'])) $state = $_GET['state']; else $state = "";
if(!empty($_GET['zip'])) $zip = $_GET['zip']; else $zip = "";
if(!empty($_GET['band'])) $band = $_GET['band']; else $band = "";
if(!empty($_GET['color'])) $color = $_GET['color']; else $color = "";
if(!empty($_GET['size'])) $size = $_GET['size']; else $size = "";
if(!empty($_GET['style'])) $style = $_GET['style']; else $style = "";
//ORDER FORM (FIRST PAGE LOAD) WITH GET VARIABLES IF AVAILABLE
echo '<form class = "form" action = "" method="post">';
echo '<h1>Order Form</h1><br>';
echo '<h4>T-Shirt Options</h4>';
echo '<label>Band: </label>';
echo '<select name="band" size="1">';
echo '<option value = "default">Choose One</option>';
$bands = array("ACDC", "Journey", "Modest Mouse", "Band of Horses", "Vampire Weekend", "Of Monsters and Men", "Broken Bells", "Phoenix", "Fleetwood Mac", "AJR");
foreach($bands as $band)
{
echo '<option value = '.$band.'> '.$band.' </option>';
}
echo '</select>';
echo '<label>Color: </label>';
echo '<select name="color" size="1">';
echo '<option value = "default">Choose One</option>';
$colors = array("Black", "Navy", "Red", "Orange", "Pink", "Yellow", "Green", "Gray", "White", "Purple");
foreach($colors as $color)
{
echo '<option value = '.$color.'> '.$color.' </option>';
}
echo '</select>';
echo '<label>Size: </label>';
echo '<select name="size" size="1">';
echo '<option value = "default">Choose One</option>';
$sizes = array ("X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large", "XXX-Large");
foreach($sizes as $size)
{
echo '<option value = '.$size.'> '.$size.' </option>';
}
echo '</select>';
echo '<label>Style: </label>';
echo '<select name="style" size="1">';
echo '<option value = "default">Choose One</option>';
$styles = array ("Tank Top", "T-Shirt", "Long Sleeve", "Hoodie", "Sweatshirt", "Jacket",);
foreach($styles as $style)
{
echo '<option value = '.$style.'> '.$style.' </option>';
}
echo '</select>';
echo '<br><br><br>';
echo '<h3>Contact Information</h3>';
echo '<label> First Name: <input type = "text" value="'.$firstName.'" placeholder = "First Name" name ="firstName"></label>';
echo '<label> Last Name: <input type = "text" value="'.$lastName.'" placeholder = "Last Name" name = "lastName"></label>';
echo '<label> Email: <input type = "text" value="'.$email.'" placeholder = "Email Address" name = "email"></label>';
echo '<label> Phone: <input type = "text" value="'.$phone.'" placeholder = "Phone Number" name = "phone"></label>';
echo '<label> Address: <input type = "text" value="'.$address.'" placeholder = "Address" name = "address"></label>';
echo '<label> City: <input type = "text" value="'.$city.'" placeholder = "City" name = "city"></label>';
echo '<label> State: <input type = "text" value="'.$state.'" placeholder = "State" name = "state"></label>';
echo '<label> Zip Code: <input type = "text" value="'.$zip.'" placeholder = "Zip Code" name = "zip"></label>';
echo '<input type = "reset" class="buttons">';
echo '<input type="submit" name="submit-button" class="buttons"> ';
echo '</form>';
if(isset($_POST['submit-button'])) //ERROR TRAPPING
{
if (in_array($_POST['band'], $bands) === false)
{$errors[] = 'Please select a band.';}
else {$bands = $_POST['band'];}
if (in_array($_POST['color'], $colors) === false)
{$errors[] = 'Please select a color.';}
else {$colors = $_POST['color'];}
if (in_array($_POST['size'], $sizes) === false)
{$errors[] = 'Please select a size.';}
else {$sizes = $_POST['size'];}
if (in_array($_POST['style'], $styles) === false)
{$errors[] = 'Please select a style.';}
else {$styles = $_POST['style'];}
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email))
{$errors[] = 'Please enter a valid email address.';}
else {$email = $_POST['email'];}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstName))
{$errors[] = 'Please enter a first name containing letters and spaces only.';}
else {$firstName = $_POST['firstName'];}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp, $lastName))
{$errors[] = 'Please enter a last name containing letters and spaces only.';}
else {$lastName = $_POST['lastName'];}
$num_exp = "/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/";
if(!preg_match($num_exp, $phone))
{$errors[] = 'Please enter a valid phone number in the following format (xxx)xxx-xxxx.';}
else {$phone = $_POST['phone'];}
$num_exp = "/^[A-Za-z0-9._%-]/";
if(!preg_match($num_exp, $address))
{$errors[] = 'Please enter an address.'; }
else {$address = $_POST['address'];}
$num_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($num_exp, $city))
{$errors[] = 'Please enter a city.';}
else {$city = $_POST['city'];}
$num_exp = "/^[A-Za-z]{2}$/";
if(!preg_match($num_exp, $state))
{$errors[] = 'Please enter a state abriviation';}
else {$state = $_POST['state'];}
$num_exp = "/^[0-9]{5}$/";
if(!preg_match($num_exp, $zip))
{$errors[] = 'Please enter a valid zip code with 5 numbers only.';}
else {$zip = $_POST['zip'];}
}
if (!empty($errors) ) //OUTPUT ERRORS IF AVAILABLE
{
foreach ($errors as $error)
{echo '<li>'.$error.'</li>';}
}
}
else
{
//IF THERE ARE ERRORS REDIRECT
if(!empty($errors) )
header('location: ?firstName='.$firstName.'&lastName='.$lastName.'&email='.$email.'&phone='.$phone.'&address='.$address.'&city='.$city.'&state='.$state.'&zip='.$zip.'&band='.$band.'&color='.$color.'&size='.$size.'&style='.$style.'"');
else //OUTPUT THANK YOU PAGE WITH CUSTOMER INFO
{
echo '<h3 class="subheaderone">Thank you for your order!</h3><br>';
echo '<h3 class = "subheadertwo">Product:</h3>';
echo '<div class = "output">';
echo '<p>'.$_POST['size']." ".$_POST['color']." ".$_POST['band']." ".$_POST['style'].'</p><br>';
echo '<br>';
echo '<h3 class = "subheadertwo">Shipping & Contact Information:</h3>';
echo '<p>Name: '.$_POST['firstName']." ".$_POST['lastName'].'</p>';
echo '<p>Email: '.$_POST['email'].'</p>';
echo '<p>Phone Number: '.$_POST['phone'].'</p>';
echo '<p>Address: </p>';
echo '<p>'.$_POST['address'].'</p>';
echo '<p>'.$_POST['city'].", ".$_POST['state']." ".$_POST['zip'].'</p><br>';
echo '</div>';
echo '<div class="another">';
echo '<nav><a href=""> Submit Another Form</a></nav>';
echo '</div>';
}
}
?>