0290280 - MacKay Wilford

Weekly assignment for lesson 2

Exercise no. 1



Please use the exercise1.php file as base for your exercise. The main objectives of this exercise are to familiarize you with the concepts of the self-processing form, string cleaning, string splitting, string display and the if (empty()) construct.

The exercise contains a mandatory part and an optional part that involves string splitting.

If you look at exercise1.php you'll notice that the submission detection and unsubmitted state have been provided for you. In order to discriminate between the unsubmitted (display of the form) and the submitted (processing of the data) states, we have used the simple request method detection technique explained in the lecture.

if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) { // Phase 1 (unsubmitted) } else { // Phase 2 (submitted) }

The form contains a text field called fullname. When this piece of data is submitted by the user, it activates the script's second (submission) phase. Your assignment is to provide code in this phase that:

  1. REQUIRED: Properly cleans the text that has been passed to the script through the $_POST['fullname'] field. (Hint: Review the "Cleaning functions" section of the lecture and the final example.)
  2. REQUIRED: Once the $_POST['fullname'] field has been cleaned, test to see if it is empty.

To see this behavior in action, look at the live example (link below). As usual, feel free use your design skills to increase the visual appeal and usability.

Quick Link

Grading (for instructors)

Exercise no. 2



Please use the exercise2.php file as base for your exercise. For a better understanding of the assignment, you may wish to run our live solution before attempting to follow the task list.

This optional exercise requires you to write a self-processing form from scratch, using the GET/POST detection method.

The script simulates a fitness assistant (heart rate monitor) which, given a person's desired maximum heart rate and selected target zone (from 1 to 5), calculates a range of heart beats per minute defined by upper and lower boundaries.

The calculation relies on the following conversion table. Each of the values is applied to the maximum heart rate. For instance, for Zone 1, given a desired maximum heart rate of 200, the script returns a lower limit of 100 (200 * 0.5) and an upper limit of 120 (200 * 0.6).

Target zone Lower limit Upper limit
Zone 1 50% 60%
Zone 2 60% 70%
Zone 3 70% 80%
Zone 4 80% 90%
Zone 5 90% 100%

The requirements for this exercise are:

  1. GET (unsubmitted) phase: Display a form with:
    1. a text field named 'mhr' which represents the maximum heart rate. It should accept a number greater than 0.
    2. a select box named 'zone' with the 5 target zones (zone1, zone2, ..., zone5)
  2. POST (submitted) phase:
    1. Clean the input fields.
    2. Check to be sure $_POST['mhr'] is not empty and is a number; if it is, properly set an error message.
    3. Check to be sure $_POST['zone'] is not empty; ditto on the error message.
  3. If there are no errors:
    1. Use a switch control structure to set the lower and upper limits for the selected target zone.
    2. Display a clear summary of the submitted data such as target zone, lower limit and upper limit. HINT: Output should be meaningful and in context. A heart either beats or it doesn't, so decimal values are not meaningful in this context.
  4. If there are errors, display a proper summary of the errors.

Quick Link

Grading (for instructors)

Exercise no. 3



Please use the exercise3.php file as base for your exercise.

This exercise is very simple. Unlike the previous exercises or real life, the ONLY purpose of this exercise is to observe the behavior of various types of form fields. They do behave differently, and it is important that you become familiar with how they act so that you will know how to process them safely and properly.

Please write a self-processing form using the usual GET/POST detection method. The form should contain the following fields:

The GET branch should display the form. The POST branch should do only one thing: Dump the submitted information using print_r() or var_dump. Don't clean anything. Don't try to validate or print anything else. This is about observation.

Now, click the "Test your exercise" button below

  1. Do NOT enter anthing into the form. Simply press the submit button.
  2. Observe the output. Write down your observations in the Completed Assignments folder along with the link to this page for this week's work.

Quick Link

Grading (for instructors)