PHP Tutorial: Form Verification And Simple Validation, A One Page script for PHP form verification.
Having used various means of verifying HTML forms I believe that this method of verifying a form to be the best mostly because it does everything on one page. It presents the form on one page and then when the submit button is pressed, if all the required fields are not filled out then it will present the form again with all the fields intact and in red lettering will point out the fields that are required to be filled out in red. It is not possible to click submit using this method even if the user has turned JavaScript off. While it is possible to use javascript to verify that all fields are filled out, if the user has turned off Javascript this method will not work any way. This is done using PHP and if you are hosted with Astahost then why not go ahead and use it. The only thing this form will not do is repopulate checkboxes since they are usually an indexed array (but don’t have to be , they could be associative) and I have another method for that but that is for later. You can take this script and modify it after seeing how it works and make it perform the way you would like for it to. This method will use both HTML and PHP in the same page so lets get started
CODE
<form action=”form.php” method=”post”>
<table border=”1″ cellpadding=”2″ bgcolor=”azure”><!–Put a nice border areound the table and add soft color–>
<tr>
<td width=”20%” align=”right”>First Name</td>
<td width=”80%”>
<input type=”text” name=”firstname” size=”20″ value=”<?php echo $_POST[firstname] ?>”></td><!–NOTICE the php in the values–>
</tr>
<tr>
<td width=”20%” align=”right”>Last Name</td>
<td width=”80%”>
<input type=”text” name=”lastname” size=”20″ value=”<?php echo $_POST[lastname] ?>”></td><!–will echo users input for repopulation–>
</tr>
<tr>
<td width=”20%” align=”right”>Username</td>
<td width=”80%”>
<input type=”text” name=”username” size=”20″ value=”<?php echo $_POST[username] ?>”> (must be between
6 an 12 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>Password</td>
<td width=”80%”>
<input type=”password” name=”password” size=”20″ value=”<?php echo $_POST[password] ?>”>
(Password must be at least 6 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>E-mail</td>
<td width=”80%”>
<input type=”text” name=”email” size=”40″ value=”<?php echo $_POST[email]; ?>”></td><!–Give more room for long emails–>
</tr>
<tr>
<td width=”20%” align=”right”> </td>
<td width=”80%”>
<input type=”submit” value=”” name=”submit”></td>
</tr>
</table>
<h3>The Username Password and the E-mail fields are required!</h3>
</form>
Using the code above as a model you can modify it to suit your needs for your own site. The regex used to validate I found at the Zend site and is meant to work with .be or .any two or three character extension in a URL I have just finished working on a script that repopulates checkbox data. After looking all over the net for a tutorial or even asking in forums to make it work, I built my own that works like I want, so if there are enough requests I will post it along with explainations and comments. It takes four pages of code to work, but two of them are almost identical it is just that one inserts data and the other updates the database.
CODE :
<?php /* this is guarunteed to work it is possible to use <? (short tags but this style works everywhere).*/
/*Only verify/validate form when it is submitted program name: form.php */
if(isset($_POST[submit])){
$error=”;//initialize $error to blank
if(trim($_POST[username])==” || strlen(trim($_POST[username])) < 6 ||strlen(trim($_POST[username])) >12){
$error.=”Please enter a username between 6 and 12 characters!<br />”; //concatenate the $error Message with a line break
}
if(trim($_POST[password])==” || strlen(trim($_POST[password]))< 6){
$error.=”Your password must be at least 6 characters in length!<br />”;//concatenate more to $error
}
if(trim($_POST[email])==”){
$error.=”An email address is required!<br />”;
}
else {
if(!eregi(”^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $_POST[email])) {
$error=”The e-mail you entered was not in the proper format!”;
}
}
if($error==”){//Hmmmm no text is in $error so do something else, the page has verified and the email was valid
// so uncomment the line below to send the user to your own success page or wherever (swap yourpage.php with your files location).
//echo “script type=\”text/javascript\”>window.location=\yourpage.php\”<script>”;
}
else{
echo “<span style=color:red>$error</span>”;
}
}
?>
That ends the PHP part of the script except for some PHP echos in the HTML section. The first line of code checks to see if the submit button has been pressed, it won’t do anything unless submit has been pressed so then the code goes right to the HTML part below thiese explainations. The next two if conditional statements check that if the user name and password meet the conditions following the if. In the case of the username if it is equal to ” (blank) OR if the length of the string after PHP has trimmed trailing whitespace is < (less than) 6 OR if the length of username is > (greater than) 12 then it will add to the $error variable and display the message in red because of the style embedded in the script. The || means OR in PHP and in the second if condition it works the same as the username only it requires at least 6 letters or letters and numbers or any printable character.
The verification and validation requires a little more explaination becuase it uses a regular expression to test for a valid email address. The first part of the email just checks to be sure that they even enter something and if they did then the else statement checks to see that the email is in a valid format namely a group or alphanumeric or printable charactersthen a “@” symbol then more alphanumeric characters and a “.”followed by alphabetic characters. the “,” seperating the regex then gives the second part with is theemail to check against. If this test fails then the user will see the form redisplayed with the message “The email you entered was not in the proper format!” will show in red.
If there are no errors the last if condition checks if the $error variable is empty or blank and if so then you would remove the comment the(//) in front of the echo “<…. and change the URL to the page you want the user to use. Finally all the concatenated
$errors are printed by the else statement. So now all that is left is to write the HTML form. and it is below and is tacked just below the code above these explainations. NOTE Just copy and paste the first section of code and then copy and paste the HTML below right after the the ?> closing tag.
CODE
<form action=”form.php” method=”post”>
<table border=”1″ cellpadding=”2″ bgcolor=”azure”><!–Put a nice border areound the table and add soft color–>
<tr>
<td width=”20%” align=”right”>First Name</td>
<td width=”80%”>
<input type=”text” name=”firstname” size=”20″ value=”<?php echo $_POST[firstname] ?>”></td><!–NOTICE the php in the values–>
</tr>
<tr>
<td width=”20%” align=”right”>Last Name</td>
<td width=”80%”>
<input type=”text” name=”lastname” size=”20″ value=”<?php echo $_POST[lastname] ?>”></td><!–will echo users input for repopulation–>
</tr>
<tr>
<td width=”20%” align=”right”>Username</td>
<td width=”80%”>
<input type=”text” name=”username” size=”20″ value=”<?php echo $_POST[username] ?>”> (must be between
6 an 12 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>Password</td>
<td width=”80%”>
<input type=”password” name=”password” size=”20″ value=”<?php echo $_POST[password] ?>”>
(Password must be at least 6 characters)</td>
</tr>
<tr>
<td width=”20%” align=”right”>E-mail</td>
<td width=”80%”>
<input type=”text” name=”email” size=”40″ value=”<?php echo $_POST[email]; ?>”></td><!–Give more room for long emails–>
</tr>
<tr>
<td width=”20%” align=”right”> </td>
<td width=”80%”>
<input type=”submit” value=”” name=”submit”></td>
</tr>
</table>
<h3>The Username Password and the E-mail fields are required!</h3>
</form>
Using the code above as a model you can modify it to suit your needs for your own site. The regex used to validate I found at the Zend site and is meant to work with .be or .any two or three character extension in a URL I have just finished working on a script that repopulates checkbox data. After looking all over the net for a tutorial or even asking in forums to make it work, I built my own that works like I want, so if there are enough requests I will post it along with explainations and comments. It takes four pages of code to work, but two of them are almost identical it is just that one inserts data and the other updates the database.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb















