For this week’s assignment in class I’m asked to:
For this activity, you will create a quick form with the following fields:First Name Last Name Date of Birth (using a mm/dd/yyyy format) State Zip Code
To continue the activity, please follow the steps below:
With the form created, create a PHP script that will take in the form values and process each value using a variable of a specific data type to best suit the commonly entered data for each field. [b]Next, create a try/throw/catch statement in your script to handle any anticipated potential errors users may perform when entering information on the form and be sure to include information to provide to the user informing them of the error that has been performed along with suggested correction for the error. For example, you have entered an invalid value for the DOB field. The format for your DOB must be mm/dd/yyyy format[/b].</blockquote>
I’ve written a a basic html form to cover the assignment, and I’ve started my php by declaring my form variables, and setting data type for my zipcode to “int”.
Here’s where things get dicey…I wrote an if statement to check for numbers in the “$zip” field, since if anyone enters letters the return is 0 on the form, I wrote it to reflect that outcome. I’ll be turning this into a try/throw/catch statement to instead return an error. I’ve got a good idea how to do that, but I don’t even know how to get started on evaluating, and enforcing the date format.
I know that I’ll need to create an if statement around checking for a “” in the 3rd and 6th position and then building a try/throw/catch around that if statement…but I can’t figure out how to build the operation that evaluates that field. Here’s what I’ve written so far…it’s super simple, but all I’m going for is basic functionality.
This is my preheader php:
[php]<?php
// create short variable names
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$dob = $_POST[‘dob’];
$state = $_POST[‘state’];
$zip = $_POST[‘zip’];
settype ($zip, ‘int’);
?>[/php]And this is my body php:
[php]<?php // details the output the user sees upon submitting the form, which includes the date, and the user information submitted
echo "Information processed: ";
echo “$firstname”;
echo " $lastname
";
echo "$dob
";
echo " $state
";
echo "$zip
";
// early start on building error handling
if ($zip ==0)
echo ‘Invalid Zip Code Entered’;?>[/php]
While I’m hoping and waiting for someone to help, I will be attempting to get that if statement into a try/trow/catch statement, but if anyone knows of a better format then please…I’ll take all the critique and help I can get.
I don’t really understand why this assignment wants me to throw an exception for an invalid date format, when most developers use a client side script to turn the user back around to the form page and highlight what they need to correct, but this is the assignment and I didn’t choose the example.
Thanks in advance for anyone patient enough to help me out with this.