Working with String Manipulation Functions - Exercise

Contact Us or call 1-877-932-8228
Working with String Manipulation Functions - Exercise

Working with String Manipulation Functions

Duration: 15 to 25 minutes.

In this exercise, you will modify the ProcessEmployee.php script you worked on earlier.

  1. Open Strings/Exercises/ProcessEmployee.php in your editor.
  2. Modify the code so that all leading and trailing whitespace is removed and HTML entities are escaped for all incoming form variables.
  3. Test the password comparison if condition by entering "0" in the Password1 text box and "-0.00" in the Password2 text box. This does not result in an error. Fix it so that an error will be returned.
  4. Modify the code so that each word of the employee's title begins with a capital letter.
  5. Modify the code so that hard returns in the Notes field are displayed in the output.
  6. To test your solution, load Strings/Exercises/AddEmployee.php in the browser and fill out and submit the form.

Solution:

Strings/Solutions/ProcessEmployee.php
<?php
	$errors = false;
	if (trim($_POST['FirstName']) == ')
	{
		$firstName = '<span style="color:red;">First name omitted.</span>';
		$errors = true;
	}
	else
	{
		$firstName = trim(htmlentities($_POST['FirstName']));
	}

	if (trim($_POST['LastName']) == ')
	{
		$lastName = '<span style="color:red;">Last name omitted.</span>';
		$errors = true;
	}
	else
	{
		$lastName = trim(htmlentities($_POST['LastName']));
	}

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

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

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

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

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

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

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

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

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

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

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

	if ( strcmp($_POST['Password1'],$_POST['Password2'])==0 )
	{
		$password = trim(htmlentities($_POST['Password1']));
	}
	else
	{
		$password = '<span style="color:red;">Passwords don\'t match.</span>';
		$errors = true;
	}
?>
---- C O D E   O M I T T E D ----
Next