Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

674 LESSON 24: Taking Advantage of the Server


I use the is_numerical() function to check if the $agecheck variable (which was
assigned the age submitted or ‘null’) is a number. If it isn’t I set the $ageErr error vari-
able, and if it is, I set the $age variable to the submitted value.
Finally, I check to make sure that the user has selected a toy. As you saw, this field is
actually a check box group, meaning that the contents of the field are submitted as an
array (assuming I’ve named the field properly). Again, I use empty() here. It works with
regular variables and arrays, and it returns true if an array contains no elements. If there
are no elements in the array, no toys were submitted, and the error message is added to
the $toysErr variable.
That is the end of my PHP script. The benefit of doing it this way is that if there are no
submitted values, the form is displayed like normal. But if any parameters are submitted
via POST, the fields are validated as I just described.

Presenting the Form
Aside from validating form submissions, one of the other important functions of server-
side processing is to prepopulate forms with data when they are presented. Many web
applications are referred to as CRUD applications, where CRUD stands for create/update/
delete. It describes the fact that the applications are used to mostly manage records in
some kind of database. If a user submits a form with invalid data, when you present the
form for the user to correct, you want to include all the data that the user entered so that
he doesn’t have to type it all in again. By the same token, if you’re writing an application
that enables a user to update his user profile for a website, you will want to include the
information in his current profile in the update form. This section explains how to accom-
plish these sorts of tasks.

Separating Presentation and Logic
The point here is that it’s common to mix PHP and HTML in this way. You create
your loop using PHP, but you define the HTML in the page rather than in echo() calls
inside your PHP code. This is generally considered the best practice for PHP. You
should write as much HTML as possible outside your PHP scripts, using PHP only
where it’s necessary to add bits of logic to the page. Then you can keep the bulk of
your PHP code at the top or bottom of your page or in included files to separate the
presentation of your data and the business logic implemented in code. That makes
your code easier to work on in the future. As an example, rather than sprinkling the
validation code throughout my page, I put it in one function so that a programmer
can work on it without worrying about the page layout. By the same token, I could
have built the unordered list inside the validation function and just returned that, but
then my HTML would be mixed in with my PHP. Cleanly separating them is generally
the best approach.


Free download pdf