In this tutorial you can learn server side validation in PHP Programming. Most of the web developer used PHP for making dynamic web application or dynamic website because it is server side scripting language. In Dynamic web application or sites user or owner can store, retrieve, add, edit and delete information or data from database.
In Programming terms validation means to check data which are entered by used via html form. In HTML form user can submit data via textbox, radio box, check box and other HTML element. Validation can be perform after the user fill up form and click on submit button. In terms of validation to check user enter proper data that matches our requirement. If data not in proper formate then user get back error message.
There are two types of validation.
Client Side Validation - In Client Side validation, validation perform on client computer by using Javascript or jquery before form data send to server.
Server Side Validation - In Server Side Validation, validation perform after the user send data to server via html form by click on submit button and data received on server side and check data on server. If you there is any data is not follow rules then server send back error message to client computer.
In this Post we have discuss server side form validation in php programming language. In Server Side Validation We will check required field must have value. For I have use php empty() function. In Name field user can only enter alphabetic letters only. For I have use preg_match() function with regular expression that check if user enter number or special character in name field it will received error message and form will not submit data to the server. In this post other validation I have discuss is that in email field user enter valid email. If client enter in valid email, so form will not be submitted and he will received error message. For this validation I have use filter_var() function. This function filter the email field and check email in valid formate or not.
This all this things I have describe in my video. You can find video on above this page, you can also watch that video and learn Server Side Form Validation in PHP in PHP Video Tutorial also.
PHP is a server side scripting language that is used by web developers to build dynamic websites or web-based applications.
PHP could be a server facet scripting language that's employed by net developers to make dynamic websites or web-based applications.
Source Code
<?php
//Above HTML
$name_error = '';
$email_error = '';
$gender_error = '';
$output = '';
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$name_error = "<p>Please Enter Name</p>";
}
else
{
if(!preg_match("/^[a-zA-Z ]*$/", $_POST["name"]))
{
$name_error = "<p>Only Letters and whitespace allowed</p>";
}
}
if(empty($_POST["email"]))
{
$email_error = "<p>Please Enter Email</p>";
}
else
{
if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
{
$email_error = "<p>Invalid Email formate</p>";
}
}
if(empty($_POST["gender"]))
{
$gender_error = "<p>Please your Gender</p>";
}
if($name_error == "" && $email_error == "" && $gender_error == "")
{
$output = '<p><label>Ouput-</label></p>
<p>Your Name is '.$_POST["name"].'</p>
<p>Your Email is '.$_POST["email"].'</p>
<p>Your Gender is '.$_POST["gender"].'</p>
';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | PHP Server Side Form Validation</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container" style="width:500px;">
<h3 class="text-center">PHP Server Side Form Validation</h3>
<form method="post">
<div class="form-group">
<label>Enter Name<span class="text-danger">*</span></label>
<input type="text" name="name" class="form-control" maxlength="75" />
<span class="text-danger"><?php echo $name_error; ?></span>
</div>
<div class="form-group">
<label>Enter Email<span class="text-danger">*</span></label>
<input type="text" name="email" class="form-control" maxlength="150" />
<span class="text-danger"><?php echo $email_error; ?></span>
</div>
<div class="form-group">
<label>Select Gender<span class="text-danger">*</span></label>
<div class="radio">
<input type="radio" name="gender" value="Male" />Male
</div>
<div class="radio">
<input type="radio" name="gender" value="Female" />Female
</div>
<span class="text-danger"><?php echo $gender_error; ?></span>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-info" />
</div>
</form>
<div><?php echo $output; ?></div>
</div>
<br />
</body>
</html>
thats great
ReplyDeleteNice article
ReplyDeletemeaningful !!
ReplyDelete