Form Validation with PHP
This post will show you how to create A User Input Form , that is capable to validate Inputted Value by User
Files involve
1.This sample contains 1 Files (1)trapError.php
Prior to that you need to complete the items listed Below
- Mamp or xampp with phpMyAdmin install into your Computer , check out here
- A Sample Database created Check out Here
- New User Created . Check out Here
- A New table created , check out Here
- Insert Data into the Table Here
trapError.php
<?php $dbSuccess=false; // Call the DB Connection PHP Script include ("C:/xampp/htdocs/dbconnection.php"); // check Whether User Enter Email Format function checkEmail($email) { $find1 = strpos($email, '@'); $find2 = strpos($email, '.'); return ($find1 !== false && $find2 !== false && $find2 > $find1); } // Connection Successful if ($dbSuccess) { echo '<h4>Create User - Trap Error FORMs:</h4>'; // Check whether the User Enter All the Input Value if (isset($_POST["firstName"]) && isset($_POST["lastName"]) && isset($_POST["yourMail"]) && isset($_POST["yourCompany"])) { // Get the all the Form Value $firstName = $_POST["firstName"]; $lastName = $_POST["lastName"]; $yourMail = $_POST["yourMail"]; $yourCompany = $_POST["yourCompany"]; // If Success Execute SQL querries // declare querries $insert_User ="INSERT INTO testtable ("."firstname ,"." lastname ,"." email ,"." companyname ". ")"."VALUES(" ."'".$firstName."',"."'".$lastName."',"."'".$yourMail."',"."'".$yourCompany."'".")"; // Check whether the User Enter a correct Email Format if ( checkEmail($yourMail) ) { if ($dbConnectionStatus->query($insert_User)) { echo 'Add User Successful<br /><br />'; } else { echo '<span style="color:red; ">FAILED to Register.</span><br /><br />'; } unset($firstName); unset($lastName); unset($yourMail); unset($yourCompany); } else{ echo '<span style="color:red; ">Please Enter an Email Format.</span><br /><br />'; } } else { //--------------------------------------------------------------------------------- echo '<form name="postUser" action="trapError.php" method="post">'; echo ' <table> <tr> <td>First Name</td> <td><input type="text" name="firstName"size="30" maxlength="10"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastName" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="yourMail" /></td> </tr> <tr> <td>CompanyName</td> <td><input type="text" name="yourCompany" /></td> </tr> <tr> <td></td> <td align="right"><input type="submit" value="Save" /></td> </tr> </table> '; echo '</form>'; //---------------------------------------------------------------------------------- } unset($firstName); unset($lastName); unset($yourMail); unset($yourCompany); echo "<br /><br />"; echo '<a href="trapError.php"> <span style="color: maroon; ">Add Another</span> </a>'; } ?>