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:
substr_count()
function)strpos
and substr
in order to split the source string into two parts (first name and family name). The names must be split cleanly. That is, there should be no extra spaces to either side of the name. (No fair using any variation of trim
to eliminate them.);
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.
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:
'mhr'
which represents
the maximum heart rate. It should accept a number greater than 0.'zone'
with the 5 target zones
(zone1, zone2, ..., zone5)switch
control structure to set
the lower and upper limits for the selected target zone.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