Input Validation
Introduction.
Whenever you are obtaining inputs from a user it is essential to have
input validation. Since I am more inclined to use JavaScript for
this since it is done on the client side before the inputs are even
submitted, we will not get into great depth on this topic in PHP.
Regular expressions, common to a number of languages at least in concept,
can greatly ease the efforts towards getting input validation. The following is a list of some different purposes for getting input validation.
While there are any number of reasons to engage in preprocessing to help ensure inputs are valid, we will quickly present a few strategies that are often used.
Again, there are many other approaches to doing these sorts of things. In this page we will focus on what is essentially testing only for whether or not particular entries have blanks or appropriate length. These will be done with two different built in functions.
We will work with a fairly simple form of text boxes and text areas, though this can be easily elaborated on. You should call the form page inputs.html. |
<html> <head> <title>HTML Form</title> </head> <body bgcolor = "664500" text="cccccc"> <form action="handle_inputs.php" method=post> <table> <tr> <td><font size = 4 color=cccccc>First Name:</font> </td> <td><input type=text name="txtFirstName" size=20> </td> </tr> <tr> <td><font size = 4 color=cccccc>Last Name:</font> </td> <td><input type=text name="txtLastName" size=20> </td> </tr> <tr> <td><font size = 4 color=cccccc>EMail Address:</font> </td> <td><input type=text name="txtEMail" size=50> </td> </tr> <tr> <td><font size = 4>Comments:</font> </td> <td><textarea name="tareaComments" rows = 5 columns = 20></textarea> </td> </tr> <tr> <td colspan = 2 align = center><input type = submit name="submit" value="submit"> </td> </tr> </table> </form> </body> </html> |
You should call the processing script handle_inputs.php. |
<html> <head> <title>Displaying the Inputted Information</title> </head> <body> <?php // obtaining the information from the form and displaying it if (isset($txtFirstName)) {
}
}
}
}
}
} print "Your comments are <b>$tareaComments </b><BR>\n"; |
Try leaving entries blank and/or putting in blanks to
see how the script processes your inputs. You should also try a
variety of other types of inputs. isset( ) tests whether a variable has a value, but not whether it is NULL or FALSE. |