Errors and $_GET Not Populating

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._%-][email protected][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>';
		}
	}
?>

I do not think you understand what the GET array does. I will try to explain…

There are two ways to get user data into your system. The most common and most direct and fairly secure way is using the POST array. This is handled like this:

POST:
Create an HTML form page with input tags. Text inputs, drodown select tags, checkboxes and radio buttons. All of these are placed into a FORM. There are also buttons that let the user section actions such as submitting the data once it is entered. Once the FORM is submitted, you use PHP to acquire all of the data using the $_POST array. Then, you validate ALL of the inputs to insure they are within your requirements. This is the most common way.

GET:
Get requires all of the data to be passed to the page using arguments. An argument would be passed to the page loosely like this: submit_page.php?arg1=‘Ernie’&arg2=123&etc=‘some value’;
Now, this means that you normally would only pass one or two items to this page since adding in a large number of arguments would be hard to do. Often, using GET is for allowing outside pages to call a function and pass a few arguments along to the code. Such as if you want to create a page to send database data to others who are allowed to access it.

Now, to check for a user’s submitting data on a page, you do it this way:

//  Check if a submit button was selected by the user...
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    //  Check if it was the submit button
    if(isset($_POST['submit-button'])) {
        //  Acquire posted data here...
        if(!empty($_GET['firstName'])) $firstName = $_GET['firstName']; else $firstName = "";
    } elseif (isset($_POST['reset'])) {
        //  User reset all values...  Do something...
    } else {
        //  Handle other buttons...
    }
}

Just a loose example. With all that explained, I suspect that you should not be using GET, but, need to use POST instead. You can use GET if you want to, but, it is not the best way. Let’s explain that one. If you want to make it nice for the user, you want to show partial inputs. This means that in your form, you need to display their partial entries and mark the missing ones. For instance, if they enter their first name but not their last name, you don’t want to display the first name so they do not need to retype it. So, you would display the data from the previously posted data. Does that make sense. And, the previously data is in the POST array and can be redisplayed with an error message for the missing data. You could do this with GET, but you would need to build the header command skipping the data variables that are missing. A complicated process and a waste of time. The POST array is easier to use!

Hope this helps… Let us know when you get stuck…

Sponsor our Newsletter | Privacy Policy | Terms of Service