Exercises: Checking form data
Exercise: Ordering collars
Create a form like this:

Figure 1. Empty form
The user enters a model number and the quantity. Model number must be either 23 or 39. Quantity must be a number greater than zero.
The data is sent to a PHP page for validation. All validation takes place in PHP!
If the data is OK, just show it:

Figure 2. Valid data
But suppose there’s an error, like no model number:

Figure 3. No model
Show an error message like this:

Figure 4. Error message
Here are the error messages your page should generate:
- Please enter the model number.
- Sorry, model must be 23 or 39.
- Please enter the quantity.
- Please enter a number for quantity.
- Please enter a quantity more than zero.
You can try my solution.
Upload your solution to your server. Put your URL below.
(Log in to enter your solution to this exercise.)
Exercise: New product form
Create a form that lets a user enter information about a new product. (Later, you’ll learn how to add this data to a database. This exercise is just about the form part.)
The form looks like this when opened:

Figure 1. Empty form
If the user leaves all the fields blank and clicks the button, show:

Figure 2. Empty form with error messages
Numbers must be valid:

Figure 3. Bad number
All of these checks are done on the client side.
There is one check that is done on the server side only: the selling price cannot be less than the purchase cost. If it is, show:

Figure 4. Purchase cost less than selling price
If everything is OK, show a confirmation page:

Figure 5. Confirmation
You can try my solution and download the files. But do it yourself first!
Here is the error icon:
The icon came from the famfamfam silk icon set. It’s one of the most complete icon sets you can find.
Hints:
- There are text input fields. Remember to use the
stripslashes()function that we talked about earlier. For example:
$product_name = stripslashes($_POST['product_name']);
print stripslashes($_POST['description']);
- Notice that when the selling price is less than the purchase price, the error message shows in the global error message area.
Upload your solution to your server. Put the URL below.
(Log in to enter your solution to this exercise.)
Exercise: Idea saver
Write a PHP application that saves ideas for projects to a file. You can try the application. (I disabled the idea saving bit.)
Here is the home page:

Figure 1. Home page
Note that the input focus is in the first field when the page loads.
You can download the background image. It’s from an OSWD template. You can also download the error icon.
The user completes all of the fields. If any fields are left blank, the user sees a page like this:

Figure 2. Missing data
Checks for missing data are done on the client.
Only some users are allowed to post ideas about some projects.
- Users
beth.jacksonandzon.plaskeare allowed to post ideas about theGrendelproject.
- Users
jed.mccurryandfhit.jhogesare allowed to post ideas about theBathronproject.
If the user is not allowed to post about the project, the following appears:

Figure 3. No permission
The message is “Sorry, that user does not have permission to post ideas about that project. Please check the project and user name.”
If the user is allowed…

Figure 4. Valid data
... the post is saved to a file, and a confirmation shown:

Figure 5. Confirmation
The project/user permission check must be done on the server! In PHP. Not in JavaScript! You can use the pattern from the Complete validation page.
Click the “Show ideas” link, and you see a list of all the ideas saved:

Figure 6. Idea list
Upload your solution to your server. Put the URL below.
(Log in to enter your solution to this exercise.)
