Processing Form Input - Exercise

Contact Us or call 1-877-932-8228
Processing Form Input - Exercise

Processing Form Input

Duration: 40 to 50 minutes.

In this exercise, you will create a page that processes the form data. The form entry page, Exercises/AddEmployee.php, is already complete and is identical to Demos/AddEmployee.php. Filled out, the form would look like this:Add Employee Form

If everything is filled out correctly, the page should display as follows:Process Employee Filled Out

If all fields are left blank except for the password fields, which contain two different values, the page should display as follows:Process Employee Not Filled Out

  1. Open Forms/Exercises/ProcessEmployee.php in your editor.
  2. For each form entry (other than the dates, which we'll worry about later), create a simple global variable to hold...
    • either an error if the entry is not filled out or filled out incorrectly.
    • or the value entered.
  3. Output the variables as list items as shown in the screenshots above.
  4. Save your work.
  5. To test your solution, load Forms/Exercises/AddEmployee.php in the browser and fill out and submit the form.

Code Sample:

Forms/Exercises/ProcessEmployee.php
<?php
	//For each entry coming through the form,
	//  create a simple global variable to hold...
	//	- either an error if the entry is not filled or filled incorrectly
	//	- or the value entered.
	//The first one is done for you.

	if ($_POST['FirstName'] == '')
	{
		$firstName = '<span style="color:red;">First name omitted.</span>';
	}
	else
	{
		$firstName = $_POST['FirstName'];
	}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Process Employee</title>
</head>
<body>
	<h1>Process Employee</h1>
	<ul>
	<?php
	//Output the variables as list items.
	//Part of the first one is done for you.
		echo "<li><b>Name:</b> $firstName</li>";
	?>
	</ul>
</body>
</html>

Challenge

If there are any errors, include a link at the bottom that directs the user to the form. If there are no errors, include a form full of hidden fields containing the values entered and a submit button that reads "Confirm".

Solution:

Forms/Solutions/ProcessEmployee.php
<?php
	if ($_POST['FirstName'] == '')
	{
		$firstName = '<span style="color:red;">First name omitted.</span>';
	}
	else
	{
		$firstName = $_POST['FirstName'];
	}
	
	if ($_POST['LastName'] == '')
	{
		$lastName = '<span style="color:red;">Last name omitted.</span>';
	}
	else
	{
		$lastName = $_POST['LastName'];
	}

	if ($_POST['Title'] == '')
	{
		$title = '<span style="color:red;">Title omitted.</span>';;
	}
	else
	{
		$title = $_POST['Title'];
	}

	if ( array_key_exists('TitleOfCourtesy',$_POST) )
	{
		$titleOfCourtesy = $_POST['TitleOfCourtesy'];
	}
	else
	{
		$titleOfCourtesy = '<span style="color:red;">
			Title of Courtesy not selected.</span>';
	}

	if ($_POST['Address'] == '')
	{
		$address = '<span style="color:red;">Address omitted.</span>';
	}
	else
	{
		$address = $_POST['Address'];
	}

	if ($_POST['City'] == '')
	{
		$city = '<span style="color:red;">City omitted.</span>';
	}
	else
	{
		$city = $_POST['City'];
	}

	if ($_POST['Region'] == '')
	{
		$region = '<span style="color:red;">Region omitted.</span>';
	}
	else
	{
		$region = $_POST['Region'];
	}

	if ($_POST['PostalCode'] == '')
	{
		$postalCode = '<span style="color:red;">Postal code omitted.</span>';
	}
	else
	{
		$postalCode = $_POST['PostalCode'];
	}

	if ($_POST['Country'] == '')
	{
		$country = '<span style="color:red;">Country omitted.</span>';
	}
	else
	{
		$country = $_POST['Country'];
	}

	if ($_POST['HomePhone'] == '')
	{
		$homePhone = '<span style="color:red;">Home phone omitted.</span>';
	}
	else
	{
		$homePhone = $_POST['HomePhone'];
	}

	if ($_POST['Extension'] == '')
	{
		$extension = '<span style="color:red;">Extension omitted.</span>';
	}
	else
	{
		$extension = $_POST['Extension'];
	}

	if ($_POST['Notes'] == '')
	{
		$notes = 'none';
	}
	else
	{
		$notes = $_POST['Notes'];
	}

	if ($_POST['ReportsTo'] == 0)
	{
		$reportsTo = '<span style="color:red;">Manager not selected.</span>';
	}
	else
	{
		$reportsTo = $_POST['ReportsTo'];
	}

	if ($_POST['Password1'] == $_POST['Password2'])
	{
		$password = $_POST['Password1'];
	}
	else
	{
		$password = '<span style="color:red;">Passwords do not match.</span>';
	}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Process Employee</title>
</head>
<body>
	<h1>Process Employee</h1>
	<ul>
	<?php
		echo "<li><b>Name:</b> $titleOfCourtesy $firstName $lastName</li>";
		echo "<li><b>Title:</b> $title</li>";
		echo "<li><b>Address:</b> $address</li>";
		echo "<li><b>City, Region Zip:</b> $city, $region $postalCode</li>";
		echo "<li><b>Country:</b> $country</li>";
		echo "<li><b>Home phone:</b> $homePhone</li>";
		echo "<li><b>Extension:</b> $extension</li>";
		echo "<li><b>Notes:</b> $notes</li>";
		echo "<li><b>Manager:</b> $reportsTo</li>";
		echo "<li><b>Password:</b> $password</li>";
	?>
	</ul>
</body>
</html>

Challenge Solution:

Forms/Solutions/ProcessEmployee-challenge.php
<?php
	$errors = false;
	if ($_POST['FirstName'] == '')
	{
		$firstName = '<span style="color:red;">First name omitted.</span>';
		$errors = true;
	}
	else
	{
		$firstName = $_POST['FirstName'];
	}
---- C O D E   O M I T T E D ----
if ($errors)
	{
		echo '<a href="javascript:history.go(-1)">Please go back to the
					form and try again.</a>';
	}
	else
	{
	?>
	<form method="post" action="CompleteProcessForm.php">
	<input type="hidden" name="FirstName" value="<?php echo $firstName ?>">
	<input type="hidden" name="LastName" value="<?php echo $lastName ?>">
	<input type="hidden" name="Title" value="<?php echo $title ?>">
	<input type="hidden" name="TitleOfCourtesy"	value="<?php echo $titleOfCourtesy ?>">
	<input type="hidden" name="Address" value="<?php echo $address ?>">
	<input type="hidden" name="City" value="<?php echo $city ?>">
	<input type="hidden" name="Region" value="<?php echo $region ?>">
	<input type="hidden" name="PostalCode" value="<?php echo $postalCode ?>">
	<input type="hidden" name="Country" value="<?php echo $country ?>">
	<input type="hidden" name="HomePhone" value="<?php echo $homePhone ?>">
	<input type="hidden" name="Extension" value="<?php echo $extension ?>">
	<input type="hidden" name="Notes" value="<?php echo $notes ?>">
	<input type="hidden" name="ReportsTo" value="<?php echo $reportsTo ?>">
	<input type="hidden" name="Password" value="<?php echo $password ?>">
	<input type="submit" value="Confirm">
	</form>
	<?php
	}
	?>
</body>
</html>
Next