# Lab 6 - Self-referring Forms w/ Data Validation

# General

This is a challenging assignment but contains some important concepts that will be used in subsequent assignments and tests. You are recommended not to put this assignment off until the last minute.

You are to create a lab6.php file, that is going to have an HTML form that refers the page back to itself and utilizes some PHP provided functions to handle data validation.

All this will be to the end of creating a dynamic temperature conversion table, based on users input.

A self-referring form with data validation has been provided for you (and was discussed in class), the content of the file can be found in lab6_lecture.txt. Take the contents of this file and place it in a new text file with a *.php extension, and you can see how it works.

<?php
$title = "WEBD2201: Sticky Form/Data Validation Example";

include "header.php";

//empty out error and result regardless of method that got you here
$error = "";
$result = "";

if($_SERVER["REQUEST_METHOD"] == "GET"){
	//default mode when the page loads the first time
	//can be used to make decisions and initialize variables
	$num = "";
}else if($_SERVER["REQUEST_METHOD"] == "POST"){
	//the page got here from submitting the form, let's try to process
	$num = trim($_POST["inputted_number"]); //the name of the input box on the form, white-space removed

	//let's do some data validation
	if(!isset($num) || $num == ""){
		//means the user did not enter anything
		$error .= "You must enter something into the text box.";
	}else if(!is_numeric($num)){
		//means the user entered something, but not a number
		//give them a detailed message
		$error .= "The value entered <u>MUST</u> be a number, you entered: " . $num;
		//empty out the invalid data
		$num = "";
	}

	if($error == ""){  //if error is an empty string
		//no errors, do the math
		$result = $num . " squared is " . ($num * $num);
	}else{
		//there were problems, concatentate the TRY AGAIN message
		$error .= "<br/>Please Try Again";

	}

}
//NOTE:
//the first two echos below show the errors or the result (these are empty the first time the page loads)
//the third of the following echo'es makes this page self-referring
//the name of the current file is outputted placed in the action of the form
//and the fourth of the following echo'es is what makes the form sticky, the
//number previously entered on the form, is automatically displayed in the value of the text input box
?>
<h2><?php echo $result; ?></h2>
<h3><?php echo $error; ?></h3>

<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="POST" >
	Enter a number: <input type="text" name="inputted_number" value="<?php echo $num; ?>" size="5" />
	<br/><input type="submit" value="Square the number" />
</form>
<?php
	include "footer.php";
?>

# Due Date

The due date for Lab 6 is found on the Important Dates page and in DC Connect. This means that you must have published your website into your development folder on the opentech server and have submitted a clickable HTML link to your website on the opentech server in the Lab 6 assessment dropbox comment section in DC Connect (see "Submission" section).

You will be given a mark out of twenty (20). You will be told of the reasons for any loss of marks, these points are to be used as constructive criticism (i.e. fix the problems as a practice for the term tests).

# Specific Requirements

  • You are to create a lab6.php, that is satisfies the requirements below.
  • lab6.php page must be validated to the XHTML Strict standard.

  • The file you are to create for this assignment should include your header.php (which will automatically incorporate your webd2201.css stylesheet) and footer.php. This will assure the new file conforms to your existing page layout. Be sure to include a link to the new lab6.php page in your header.php file, and a detailed paragraph describing what the page does/instructions for the data to be entered.

  • Similar to previous assignments, include PHP (i.e. /* c-style */) comments with your name and course code in the lab6.php file created for this assignment. Additionally, add dynamic HTML comments to all your lab6.php page (this should occur if you implement your header.php file correctly).

  • The lab6.php is a PHP page that:

    • Defines a constant named MAX_ITERATIONS that contains a value of 100.
    • Contains a self-referencing/self-submitting form(to achieve this the action attribute on the page's <form> tag should have <?php echo $_SERVER['REQUEST_URI']; ?> or <?php echo $_SERVER['PHP_SELF']; ?>)

    • The form should be submitted in POST mode

    • This page should, the first time the pages loads in the default GET mode, create empty string variables named $start, $stop, $incr, $error and $output (respectively used to store: the start temperature; the stop temperature; the temperature increment; any error messages generated by the data validation; and, the generated table output).

    • Has three (3) named text input boxes, one for a starting temperature, one for an ending temperature and one for the increment the user wants to increase the temperature by each.

    • When submitted in POST mode, should store the three (3) inputted values from the $_POST[] array in variables named $start, $stop, and $incr

      • Performs a check that the data submitted is valid. Valid data means:
      • That the user entered something in each field (i.e. they are not empty)
      • That the user entered numbers in each field
      • That the start temperature is less than the end temperature
      • That the temperature increment is a positive (non-zero) number
      • If all the above conditions are met, you should verify that there would not be more than MAX_ITERATIONS number of loops, based on the conditions set by the user. NOTE: this can be determined by making sure that ($stop - $start)/$incr is less than or equal to MAX_ITERATIONS (after all other data validation is completed)

If any of these conditions are not met, an appropriate message should be placed in the $error variable and no calculation should be done or a table created, but the error message should be displayed. If any invalid data is entered, it should be incorporated as part of the error, but the variable subsequently should be emptied off of the form (i.e. set the variable that is echo'ed in the form input's value equal to ""). NOTE: for full marks, your page MUST detect and display multiple errors (i.e. if a non-numeric input was entered for the start temperature AND nothing was entered for an increment BOTH issues should be flagged as a problem, this can be achieved by concatenating new errors to the end of the $errors variable).

  • If there are no errors, then the page should create an $output variable that contains a table of temperature conversion (be sure to use the file that performs the calculation in a function), that uses the entered numbers and a loop.
  • To see how the page should work check out the example provided.

NOTE

The form on the page should be "sticky", which means if vaild data was placed in the form, it will still be in the form when the page reloads. This can be achieved by echoing the variables from the $_POST[] array into the value attribute of the appropriate input box.

# Rubric

Topic Description Weighting
Submission
Submission
-2 files not published into your /var/www/html/webd2201/userid main folder (instead in /home/userid folder).

-2 no link submitted in DC Connect Lab 6 assignment drop box

-1 link in DC Connect Lab 6 assignment drop box not clickable

-1 link in DC Connect Lab 6 assignment drop box does not go to your website

/2
Deliverable Specific Outcomes
Comments/Description paragraphs
-3 <!-- HTML Comments --> are not dynamic (i.e. hard-coded once and/or do not echo declared variables ($file, $date, $desc, $title, $banner) in the header.php file.

-2 dynamic <!-- HTML Comments --> not echo'ed in the header.php file (implemented incorrectly)

-2 lab6.php missing description paragraph

-1 lab6.php description paragraph light on content/not very descriptive.

-2 missing /* c-style */ comments on lab6*.php page.

/3
lab6.php Page
-1 file not named lab6.php.

-1 no link added in header.php nav bar to link to new lab6.php page.

/2
Define/Use MAX_ITERATIONS constant
-3 did not define MAX_ITERATIONS constant or use it at all: define("MAX_ITERATIONS", 100);

-1 did not set MAX_ITERATIONS to 100 when defining it: define("MAX_ITERATIONS", 100);

-2 did not verify that ($stop - $start)/$incr causes less than or exactly 100 iterations.

-1 did not use MAX_ITERATIONS in
($stop - $start)/$incr >= MAX_ITERATIONS.

-1 did not use MAX_ITERATIONS in the error message for too many iterations (hard-coded the value)

/3
Data Validation
-1 did not verify the start temperature was not empty (that the user entered something).

-1 did not verify the start temperature entered was a number using is_numeric() function.

-1 did not verify the stop temperature was not empty (that the user entered something).

-1 did not verify the stop temperature entered was a number using is_numeric() function.

-1 did not verify the temperature increment was not empty (that the user entered something).

-1 did not verify the temperature increment entered was a number using is_numeric() function.

-1 when all input were verified as numbers, did not verify the stop temperature was greater than the start temperature

-1 when all input were verified as numbers, did not verify the temperature increment was a positive number

-1 when all input were verified as numbers, did not verify the temperature increment was not zero (0)

/6
Error message and form "stickiness"
-2 did not code separate if...else statements (one for each text input) to handle multiple errors per form submission

-1 error message(s) are not specific (i.e. You must enter a start temperature... vs. You must enter something into the text box.)

-1 error message(s) did not include invalid data as part of the output.

-2 form is not "sticky" (i.e. valid data should remain on the form by echoing variables into the value attribute of the appropriate text input element)>

-1 $error error message was not echo'ed above the form.

-1 invalid data is not emptied off of the form.

/3
Output generation and display
-1 output genreated even if there are errors on the page.

-2 no $output generated using a for... loop (hard-coded output)

-1 did not use string concatenation operator to add each temperature conversion from the for loop:$output .= "<tr><td>..."

-0.5 no &deg; symbol in the output.

-1 $output variable was not echo'ed below the form.

/2
Other Penalties
XHTML Validation
-1 no clickable XHTML image provided on lab6.php page.

-2 <!DOCTYPE> in header.php was not Strict (used Transitional or HTML 4.01 instead)

-1 exactly one XHTML error on lab6.php page.

-2 more than one XHTML error on lab6.php page.

-2

(-1.0 per incident, maximum 2 incidents)

Late Submission
-0 lab submitted on time

-5 lab submitted late but within 72 hours of due date

-20 lab not submitted within 72 hours of due date

/5
header.php and footer.php Implementation
-1 lab6.php page does not implement header.php

-1 lab6.php page does not implement footer.php files.

-4

(-2.0 per incident, maximum 2 incidents)

# Resources

To perform the data validation you will want to use some of the PHP provided functions:

  • is_numeric() to determine if a number was entered
  • isset() to determine if the form data exists on the $_POST[ ] array
  • trim() to remove any white space the user might have entered
  • define() defines a named constant.
  • abs() to determine the absolute value of a number

Additionally students should check out the $_SERVER[ ] array in the manual, looking specifically at the $_SERVER['REQUEST_URI'] or $_SERVER['PHP_SELF'] as a way of submitting a form to itself, and $_SERVER['REQUEST_METHOD'] to determine whether the page loads in "GET" mode (when the page first loads vs. "POST" which means the form has been submitted for processing.

# Submission

Publish your pages on the opentech server and submit the URL of your index page as a clickable link in the comment section of the "Assignment 6" drop box in DC Connect. If you do not submit something into DC Connect, your professor will assume you did not complete the assignment, and the late penalties will apply (including 0/20 if the link was not submitted within the 72 hours late period). The onus is on you to ensure your lab instructor knows that you have work to be assessed.

WARNING

Do not mail or attach your files themselves into DC Connect. They must be published onto the opentech server.

Last Updated: 5/25/2019, 12:29:14 PM