Exercises
Goals:
· Gain more hands-on with using PHP string functions
· Create and integrate forms with PHP scripts.
Notes:
(1) Be sure to include comments in your code and always put your name and date within
your scripts. You should now be using proper indenting style for conditional
expressions and loops. I will post a style sheet on Moodle in the first section of the course
before the week_1 section.
(2) You will be modifying the HTML form and processing PHP script that you created in
Lab 3. Please copy your lab3 files (isp_form.html and isp_process.php) to a new
subdirectory e.g. Lab 4 and modify them there.
(3) There is a set of Power Point Slides called 04_Validation_Functions.ppt that will
contain most of the information you will need for this lab.
(4) Follow the suggestions in the slides to create a $errors array. Fill the array with errors as
they occur and then print them out after each step in the validation process.
Step 1: Name field validation
Modify the Name field on your ISP form so that is now two text fields – one for ‘First Name and
one for ‘Last Name’.
Lab 4 – Forms, Loops and Strings
In your isp_process.PHP script, add conditional tests to validate the data entered by the user.
Verify that first and last name are not blank. You should use the empty() function.
· Verify that the first and last name only contain the alphabetic characters [A-Za-z]. I’d
like you to try two different ways:
o For the First Name, try using preg_match() // see slides for info
o For the Last Name, try using ctype_alpha() // see slides for info
· In the PHP processing script, concatenate the $first_name and $last_name into one
variable named $fullname. Then when you echo back the length of the name, you can be
counting the characters in $full_name.
Step 2: Email Address
· Add another text box to your form and label it “Email Address”.
· Set a size of the text box = 60 characters for the email address.
· Use a PHP function to validate the email address.
· If the email address passes validation, echo it back to the user.
Step 3: Password Fields and Validation
· Add two password fields to your ISP form.
· The first password field should be labeled “Enter Password”.
· The second password field should be labeled “Confirm Password”.
· Make that when you create the password field that you define it as type = ‘password’.
When you test the program, verify that the password meets all of the conditions below. For the
last 4 conditions, you may use the regular expression from the notes. It’s quite involved and
tricky to read, but it works.
· The two passwords match
· The password is at least 8 characters long.
· The password contains at least one digit.
· The password contains at least one capital letter.
· The password contains at least one special character, e.g. !@#$%^&*()
· If the password passes validation tests, echo it back to the user.
If the password does not meet any of these conditions, print an error message indicating the
violation.
Lab 4 – Forms, Loops and Strings
Step 4: Processing String Tokens in an Address
Add another text box to the form you created for ISP Usage. The label for the text box should be
“Address”. It should accommodate up to 60 characters. The text string you enter should be in the
following format:
1024 Main St., Unit 502, Montpelier, VT 02044 (no comma after the state)
Your job is to break the string into “tokens”. For example we’d like to be able to store each part
of the address in a separate variable or position in an array and print each variable on a separate
line like this:
1024 Main St.
Unit 502
Montpelier
VT 02044
Part A: First try the explode() function to break the string into components. Then try the
implode() function to put them back together again into a single string. Notice that explode()
will not allow you to separate the state from the zip because there is no comma.
Your program should print out the contents of each variable as shown above and then the entire
address “reconstructed” as a single string again.
Part B: Now try using a different set of string functions that will allow you to also separate the
state and zip code as well as the other address elements.
Store the each component of the address in an associative array. The keys to your array
should be:
Addr_1
Addr_2
City
State
Zip
While you could process the address string character by character, try using strtok() or some
other string functions. Use a foreach loop to print out the contents of the associative array.
Lab 4 – Forms, Loops and Strings