Form validation is very important in every website to check user input correctly Enter data or leave input fields empty.So to avoid website hacking or malicious data you have to check data input by the user. In this tutorial i will show you the process of web forms validation in php and html.
First create a form with html and then validate with php code to check secure form data. The form will contain number of inputs fields, and a submit button. The input will be checked to insert data into database.
Also Check: How to insert data into Database.
For example if user leaves fields empty then an error message will be show to user to fill the form filed. First check Html form this form will grab the data enter by the user. The Html form input fields have many attribute but some are important which are type and name attribute. Define these attribute the name attribute is used to specify a name for the value. The value attribute will be submitted to the server.There are two attribute that need to be set in the form tag, action and method.
Html Code to Create Form
<form method="post" action="">
<div class="form-group col-md-6 col-sm-6">
<label>Test Code</label><span id="Test_code_error"></span>
<input name="test_code" type="text" class="form-control span12" id="test_code" size="10" value="">
</div>
<div class="form-group col-md-6 col-sm-6">
<label>Test Name</label><span id="Test_name_error"></span>
<input name="test_name" type="text" class="form-control span12" id="test_Name" value="">
</div>
<div class="form-group col-md-6 col-sm-6">
<label>Rate</label><span id="Rate_error"></span>
<input name="rate" type="text" class="form-control span12" id="rate" value="">
</div>
<div class="form-group col-md-6 col-sm-6">
<label>Sample</label><span id="Sample_error"></span>
<input name="sample" type="text" class="form-control span12" id="sample" value="">
</div>
<div class="form-group col-md-12">
<label>Reporting Time</label><span id="Reporting_time_error"></span>
<input name="reporting_time" type="text" class="form-control span12" id="reporting_time" value="">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary pull-right btn-submit" value="Update" name="submit" id="submit">
</div>
<div class="clearfix"></div>
</form>
PHP Code to Validate Form
We create a Html form now let start form validation in php and to secure form. Also check How to Create custom html contact form in php.First we will check in php when a user submit a blank form then show an error message. Second we will check that user fill the form and submit. Now we need to add php code to pick form values enter by the user. when the form submit completely show a successful message to the user by header to redirect to same page and pass a message to the page. we use some Php function in the php form validation code like htmlspecialchar() which is use to convert the predefined character to the html entities and secure web application from XSS( cross site scripting) like "<" and ">". if(isset($_POST['submit']))
{
if(empty($_POST['test_code']) || empty($_POST['test_name']) || empty($_POST['rate']) || empty($_POST['sample']) || empty($_POST['reporting_time']))
{
$error="Please Fill All the fields";
}
else
{
$test_code=mysql_real_escape_string(htmlentities(ltrim($_POST['test_code'])));
$test_name=mysql_real_escape_string(htmlentities(ltrim($_POST['test_name'])));
$rate=mysql_real_escape_string(htmlentities(ltrim($_POST['rate'])));
$sample=mysql_real_escape_string(htmlentities(ltrim($_POST['sample'])));
$reporting_time=mysql_real_escape_string(htmlentities(ltrim($_POST['reporting_time'])));
mysql_query("insert into `test_info` values('','$test_code','$test_name','$rate','$sample','$reporting_time')") or die();
header("Location:test_info.php?msg=Record Added Successfully");
}
}
some of the predefined character are
& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes <
> (greater than) becomes >
Ltrim function ltrim() is used to remove the spaces or character from the left side of a string.
mysql_real_escape_string() function is used to escape special characters in a string for use in a SQL Statement. isset is used to check if submit button is click by the user then execute the following code.
Validation is very important and essential for any web application to ensure correct and accurate data enter by the user.
In this tutorial you learn how to secure from validation in php. Along with the number of techniques to show error and some important php function.
Blogger Comment
Facebook Comment