webtechnologies

6
PHP Programming with MySQL (second edition) by Don Gosselin, Diana Kokoska, Robert Easterbrooks (ISBN-13: 978-0-5387-4584-0, ISBN-10: 0-5387-4584-3) Reference: http://www.php.net/manual/en/langref.php Chapter 4 – Handling User input 1. Create a Web form to help in creating “Jumble” puzzles. Create a form that has four input fields named Word1, Word2, Word3, and Word4, as well as “Reset” and “Submit” buttons. Create a form processing script that verifies that all four words are entered, and that all four are between 4 and 7 characters long. Once all the words have been verified as correct, use the strtoupper() and str_shuffle() functions to produce four jumbled sets of letters. (two-part form) a. Create a new document in your text editor. Type the <!DOCTYPE> declaration, <html> element, header information, and <body> element. b. Add the following XHTML form tags in the document body: <form action="chapter_04_hw_01_process.php" method="post"> Word 1: <input type="text" name="Word1" /><br /> Word 2: <input type="text" name="Word2" /><br /> Word 3: <input type="text" name="Word3" /><br /> Word 4: <input type="text" name="Word4" /><br /> <input type ="reset" value = "Clear Form" />&nbsp; &nbsp;<input type="submit" name="Submit" value="Send Form" /> </form> c. Save the document as chapter_04_hw_01.html.

Upload: kaladher-reddy

Post on 26-May-2017

238 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: webtechnologies

PHP Programming with MySQL (second edition) by Don Gosselin, Diana Kokoska, Robert Easterbrooks (ISBN-13: 978-0-5387-4584-0, ISBN-10: 0-5387-4584-3)

Reference: http://www.php.net/manual/en/langref.php

Chapter 4 – Handling User input

1. Create a Web form to help in creating “Jumble” puzzles. Create a form that has four input fields named Word1, Word2, Word3, and Word4, as well as “Reset” and “Submit” buttons. Create a form processing script that verifies that all four words are entered, and that all four are between 4 and 7 characters long. Once all the words have been verified as correct, use the strtoupper() and str_shuffle() functions to produce four jumbled sets of letters. (two-part form)a. Create a new document in your text editor. Type the <!DOCTYPE>

declaration, <html> element, header information, and <body> element. b. Add the following XHTML form tags in the document body:

<form action="chapter_04_hw_01_process.php" method="post">Word 1: <input type="text" name="Word1" /><br />Word 2: <input type="text" name="Word2" /><br />Word 3: <input type="text" name="Word3" /><br />Word 4: <input type="text" name="Word4" /><br /><input type ="reset" value = "Clear Form" />&nbsp; &nbsp;<input type="submit" name="Submit" value="Send Form" /></form>

c. Save the document as chapter_04_hw_01.html.d. Create a new document in your text editor. Type the <!DOCTYPE>

declaration, <html> element, header information, and <body> element.e. Add the opening and closing tags for the PHP script section in the body of the

document:

<?php?>

f. Add the displayError() function to the script section. This function displays the error message, and takes two parameters: $fieldName, which is the name of the field as it appears on the Web form; and $errorMsg, which describes the error for the user. There is no return value for this function.

function displayError($fieldName, $errorMsg) {global $errorCount;echo "Error for \"$fieldName\": $errorMsg<br />";++$errorCount;

Page 2: webtechnologies

}

g. Create a second function called validateWord() below the displayError() function. This function takes two parameters. The first parameter, $data, is a string to be validated. The second parameter, $fieldName, is the name of the form field. The function returns the $data parameter after it has been cleaned up. Notice that the function uses the global variable $errorCount.

function validateWord($data, $fieldName) {global $errorCount;if (empty($data)) {

displayError($fieldName, "This field is required");$retval = "";

} else { // only clean up the input if it isn't empty$retval = trim($data);$retval = stripslashes($retval);if ((strlen($retval) < 4) || (strlen($retval) > 7)) {

displayError($fieldName, "Words must be at least four and at most seven letters long");

} if (preg_match("/^[a-z]+$/i", $retval) == 0) {

displayError($fieldName, "Words must be only letters");}

}$retval = strtoupper($retval);$retval = str_shuffle($retval);return($retval);

}

h. Immediately after the validateWord() function, declare and initialize a new variable called $errorCount and a new array called $words[] as follows:

$errorCount = 0;$words = array();

i. Add assignment statements for the $words array variable to receive the output of the validateWord() function for each form field:

$words[] = validateWord($_POST['Word1'], "Word 1");$words[] = validateWord($_POST['Word2'], "Word 2");$words[] = validateWord($_POST['Word3'], "Word 3");$words[] = validateWord($_POST['Word4'], "Word 4");

j. Add a conditional statement immediately after the values of $words have been assigned. This statement will display the total number of errors found or the shuffled words if there were no errors.

Page 3: webtechnologies

if ($errorCount > 0) {echo "Please use the \"Back\" button to re-enter the data.<br />";

} else {$wordnum = 0;foreach ($words as $word) {

echo "Word " . ++$wordnum . ": $word<br />";}

}

k. Save the program as chapter_04_hw_01_process.php.l. Test the program.

2. Create an All-in-One form that calculates an employee’s weekly gross salary, based on the number of hours worked and an hourly wage that you choose.a. Create a new document in your text editor. Type the <!DOCTYPE>

declaration, <html> element, header information, and <body> element. b. Add the following script section to the document body:

<?php?>

c. Create and initialize a Boolean variable called $DisplayForm, which will be used to determine if the Web form should be redisplayed, and two string variables called $HoursWorked and $HourlyRate:

$DisplayForm = TRUE;$HoursWorked = "";$HourlyRate = "";

d. Add the following code to check whether the form data has been entered. If it has, the data will be validated:

if (isset($_POST['Submit'])) {$HoursWorked = $_POST['HoursWorked'];$HourlyRate = $_POST['HourlyRate'];if (is_numeric($HoursWorked) && is_numeric($HourlyRate)) {

$DisplayForm = FALSE;} else {

echo "<p>Number of hours worked and Hourly rate are numeric values.</p>";

$DisplayForm = TRUE;}

}

Page 4: webtechnologies

e. Add the following code to display the form, including the entered value for the number of hours worked and hourly rate fields. Note the use of advanced embedding of XHTML.

if ($DisplayForm) {?>

<form name="WeeklySalary" action="chapter_04_hw_02.php" method="post">

<p>Number of Hours worked: <input type="text" name="HoursWorked" value="<?php echo $HoursWorked; ?>" /></p>

<p>Hourly Rate: <input type="text" name="HourlyRate" value="<?php echo $HourlyRate; ?>" /></p>

<p><input type="reset" value="Clear Form" />&nbsp;&nbsp;<input type="submit" name="Submit" value="Send Form" /></p></form>

<?php}

f. Add an else clause to use the form data once it is entered correctly, as follows:

else {echo "<p>Thank you for complete your weekly salary form.</p>";if ($HoursWorked > 40) {

$ExtraPay = ($HoursWorked - 40) * 1.5 * $HourlyRate;$RegularPay = 40 * $HourlyRate;

} else {$ExtraPay = 0;$RegularPay = $HoursWorked * $HourlyRate;

}$TotalWeeklyPay = $RegularPay + $ExtraPay;echo "<p>Your Weekly Gross Pay is: " . round($TotalWeeklyPay, 2) .

"</p>";echo "<p>Number of Hours worked: $HoursWorked.</p>";echo "<p>Hourly Rate: $HourlyRate.</p>";echo "<br /><br /><p><a href=\"chapter_04_hw_02.php\">Try

again?</a></p>";}

g. Save the document as chapter_04_hw_02.php, and Test the program.