Skip to main content
registration form server side validation php

Complete Registration Form Server Side Validation In PHP

In the previous post, we saw that registration form validation using javascript and registration form validation using jQuery. Both are very important, but when we deal with more important database then we must have server side validation on our website or website. In this tutorial you will learn complete registration form server side validation in PHP. So let’s start.

First of all, we should have an HTML registration form on which we can do this tutorial. See the below code, It is an HTML form. We can do server side validation in PHP.

This is style code of html form. We can make it very beautiful using css style

style.css

.form-table
{
	width:400px;
	margin-left: auto;
	margin-right: auto;
}

label{
	font-weight: bold;
}

#form_submission_ajax{
	background-color: #eee;
	padding-top: 10px;
	padding-bottom: 10px;
}

.error{
	color: #ff0000;
}

This is an HTML code for registration form. Please read it, understand it and practice it. Store it in the index.php

index.php

<!DOCTYPE html>
<html>
<head>
<title>Demo Form validation PHP</title>
<link rel="stylesheet" type="text/css" href="../style.css">
<script type="text/javascript" src="jquery-1.11.1.js"></script>
</head>
<body>

<?php 
session_start();

$error = [
"f_name_error" => '',
"l_name_error" => '',
"email_error" => '',
"password_error" => '',
"re_password_error" => '',
"gender_error" => '',
"country_error" => '',
"language_error" => '',
"phone_error" => ''
];

$form_data = [
"fname" => '',
"lname" => '',
"email" => '',
"password" => '',
"re_password" => '',
"gender" => '',
"language" => '',
"country" => '',
"phone" => ''
];

if(!empty($_SESSION['error']))
{
	$error = $_SESSION['error'];
}

if(!empty($_SESSION['form_data']))
{
	$form_data = $_SESSION['form_data'];
}

?>

<h1><center>Server Side form validation using PHP</center></h1>
<form action="submit.php" method="post" id="form_submission_ajax">
	<table class="form-table">
		<tr>
			<td><label>Firstname:</label></td>
			<td><input type="text" name="f_name" id="f_name" value="<?php echo $form_data['fname']; ?>">
			</td>
		</tr>
		<tr>
			<td></td>
			<td id="f_name_error" class="error"><?php echo $error['f_name_error']; ?></td>
		</tr>
		<tr>
			<td><label>Lastname:</label></td>
			<td><input type="text" name="l_name" id="l_name" value="<?php echo $form_data['lname']; ?>"></td>
		</tr>
		<tr>
			<td></td>
			<td id="l_name_error" class="error"><?php echo $error['l_name_error']; ?></td>
		</tr>
		<tr>
			<td><label>Email:</label></td>
			<td><input type="email" name="email" id="email" value="<?php echo $form_data['email']; ?>"></td>
		</tr>
		<tr>
			<td></td>
			<td id="email_error" class="error"><?php echo $error['email_error']; ?></td>
		</tr>
		<tr>
			<td><label>Password:</label></td>
			<td><input type="password" name="password" id="password" value="<?php echo $form_data['password']; ?>"></td>
		</tr>
		<tr>
			<td></td>
			<td id="password_error" class="error"><?php echo $error['password_error']; ?></td>
		</tr>
		<tr>
			<td><label>Retype Password:</label></td>
			<td><input type="password" name="re_password" id="re_password" value="<?php echo $form_data['re_password']; ?>"</td>
		</tr>
		<tr>
			<td></td>
			<td id="re_password_error" class="error"><?php echo $error['re_password_error']; ?></td>
		</tr>
		<tr>
			<td><label>Gender:</label></td>
			<td>
				<input type="radio" name="gender" id="gender" class="gender" value="male" >Male
				<input type="radio" name="gender" id="gender" class="gender" value="female" >Female
			</td>
		</tr>
		<tr>
			<td></td>
			<td id="gender_error" class="error"><?php echo $error['gender_error']; ?></td>
		</tr>
		<tr>
			<td><label>Known Language:</label></td>
			<td>
				<input type="checkbox" name="language[]"  class="language" value="hindi">Hindi
				<input type="checkbox" name="language[]"  class="language" value="english">English
				<input type="checkbox" name="language[]"  class="language" value="gujarati">Gujarati
			</td>
		</tr>
		<tr>
			<td></td>
			<td id="language_error" class="error"><?php echo $error['language_error']; ?></td>
		</tr>
		<tr>
			<td><label>Country:</label></td>
			<td>
				<select name="country" id="country">
					<option value="">Select Country</option>
					<option value="IN">India</option>
					<option value="UK">United Kingdom</option>
					<option value="US">United State</option>
				</select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td id="country_error" class="error"><?php echo $error['country_error']; ?></td>
		</tr>
		<tr>
			<td><label>Phone No:</label></td>
			<td><input type="telephone" name="phone" id="phone" value="<?php echo $form_data['phone']; ?>"></td>
		</tr>
		<tr>
			<td></td>
			<td id="phone_error" class="error"><?php echo $error['phone_error']; ?></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submit" value="Submit"></td>
		</tr>
	</form>
</body>
</html>

<script type="text/javascript">
$(document).ready(function(){

/*code for gender*/
var form_gender = '';
form_gender = "<?php echo $form_data['gender']; ?>";
$('.gender').each(function(i){
	if($(this).val()==form_gender)
	{
		$( this ).attr( 'checked', true )
	}
});

/*code for language*/
var form_language = <?php echo json_encode($form_data['language']); ?>;
for(i=0;i<=form_language.length;i++)
{
	$(".language").each(function(n){
		if(form_language[i] == $(this).val())
		{
			$(this).attr( 'checked', true );		
		}
	})
}

/*code for country*/
var form_country = "<?php echo $form_data['country']; ?>";
$('#country').val(form_country);

});
</script>

<?php 
$_SESSION['error'] = "";
$_SESSION['form_data'] = "";
?>

I have to include almost every form element in this tutorial and give you solution that how to handle it. Still if you have any kind of problem then let me know in the comment box.

Now we have to create a “submit.php” file for server side validation using PHP. In this file we check the form element value and if it not as per our requirement then it will redirect to home page we respective errors. Let’s see practically.

<?php 
session_start();
$valid = true;
$error = [];
$form_data = [];
if(!empty($_POST['f_name']))
{
	$f_name = $_POST['f_name'];
	$fname_data = array("fname" => $f_name);
	$form_data = array_merge($form_data, $fname_data);
	$f_name_error = array("f_name_error" => "");
	$error = array_merge($error, $f_name_error);
}
else
{
	$valid = false;
	$f_name = "";
	$fname_data = array("fname" => $f_name);
	$form_data = array_merge($form_data, $fname_data);
	$f_name_error = array("f_name_error" => "* Firstname is required.");
	$error = array_merge($error, $f_name_error);
}

if(!empty($_POST['l_name']))
{
	$l_name = $_POST['l_name'];
	$lname_data = array("lname" => $l_name);
	$form_data = array_merge($form_data, $lname_data);
	$l_name_error = array("l_name_error" => "");
	$error = array_merge($error, $l_name_error);
}
else
{
	$valid = false;
	$l_name = "";
	$lname_data = array("lname" => $l_name);
	$form_data = array_merge($form_data, $lname_data);
	$l_name_error = array("l_name_error" => "* Lastname is required.");
		$error = array_merge($error, $l_name_error);
}

if(!empty($_POST['email']))
{
	$email = $_POST['email'];
	$email_data = array("email" => $email);
	$form_data = array_merge($form_data, $email_data);
	$email_error = array("email_error" => "");
	$error = array_merge($error, $email_error);
	if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
	{
  		$email_error = array("email_error" => "* Valid emial format is 'abc@xxxx.com'.");
		$error = array_merge($error, $email_error);
	}
}
else
{
	$valid = false;
	$email = "";
	$email_data = array("email" => $email);
	$form_data = array_merge($form_data, $email_data);
	$email_error = array("email_error" => "* Email is required.");
	$error = array_merge($error, $email_error);
}

if(!empty($_POST['password']))
{
	$password = $_POST['password'];
	$password_data = array("password" => $password);
	$form_data = array_merge($form_data, $password_data);
	$password_error = array("password_error" => "");
	$error = array_merge($error, $password_error);
}
else
{
	$valid = false;
	$password = "";
	$password_data = array("password" => $password);
	$form_data = array_merge($form_data, $password_data);
	$password_error = array("password_error" => "* Password is required.");
	$error = array_merge($error, $password_error);
}

if(!empty($_POST['re_password']))
{
	$re_password = $_POST['re_password'];
	$re_password_data = array("re_password" => $re_password);
	$form_data = array_merge($form_data, $re_password_data);
	$re_password_error = array("re_password_error" => "");
	$error = array_merge($error, $re_password_error);
}
else
{
	$valid = false;
	$re_password = "";
	$re_password_data = array("re_password" => $re_password);
	$form_data = array_merge($form_data, $re_password_data);
	$re_password_error = array("re_password_error" => "* Re-Password is required.");
	$error = array_merge($error, $re_password_error);
}

if($password != $re_password)
{

}

if(!empty($_POST['gender']))
{
	$gender = $_POST['gender'];
	$gender_data = array("gender" => $gender);
	$form_data = array_merge($form_data, $gender_data);
	$gender_error = array("gender_error" => "");
	$error = array_merge($error, $gender_error);
}
else
{
	$valid = false;
	$gender = "";
	$gender_data = array("gender" => $gender);
	$form_data = array_merge($form_data, $gender_data);
	$gender_error = array("gender_error" => "* Gender is required.");
	$error = array_merge($error, $gender_error);
}

if(!empty($_POST['language']))
{
	$language = $_POST['language'];
	$language_data = array("language" => $language);
	$form_data = array_merge($form_data, $language_data);
	$language_error = array("language_error" => "");
	$error = array_merge($error, $language_error);
}
else
{
	$valid = false;
	$language = "";
	$language_data = array("language" => $language);
	$form_data = array_merge($form_data, $language_data);
	$language_error = array("language_error" => "* Language is required.");
	$error = array_merge($error, $language_error);
}

if(!empty($_POST['country']))
{
	$country = $_POST['country'];
	$country_data = array("country" => $country);
	$form_data = array_merge($form_data, $country_data);
	$country_error = array("country_error" => "");
	$error = array_merge($error, $country_error);
}
else
{
	$valid = false;
	$country = "";
	$country_data = array("country" => $country);
	$form_data = array_merge($form_data, $country_data);
	$country_error = array("country_error" => "* Country is required.");
	$error = array_merge($error, $country_error);
}
 
if(!empty($_POST['phone']))
{
	$phone = $_POST['phone'];
	$phone_data = array("phone" => $phone);
	$form_data = array_merge($form_data, $phone_data);
	$phone_error = array("phone_error" => "");
	$error = array_merge($error, $phone_error);
	if(!preg_match('/^\d{10}$/',$phone))
	{
		$phone_error = array("phone_error" => "* Valid phone number format is 0987654321.");
		$error = array_merge($error, $phone_error);
	}
}
else
{
	$valid = false;
	$phone = "";
	$phone_data = array("phone" => $phone);
	$form_data = array_merge($form_data, $phone_data);
	$phone_error = array("phone_error" => "* Phone number is required.");
	$error = array_merge($error, $phone_error);
}

$_SESSION['error'] = $error;
$_SESSION['form_data'] = $form_data;
header('Location:index.php');

In the above file we can check all the form elements and through errors if it not as per our condition. All the errors are collected in single array and store in the session. When this file redirect to index.php file. We will use this session to show errors to the user. I think it is very simple. If you have any kind of problem regarding this tutorial please let me know your questions in the comment box i will give you answers. Thank you everyone. Please share this tutorial with your friend and discuss the questions and answers. 🙂

admin

HI, i am software developer having knowledge of PHP, C, C++ programming languages. I am very passionate about programming and ability to solve the programming problems. CodeandTuts is my blog where i like to put new tutorial and help new developer.

Leave a Reply

Your email address will not be published. Required fields are marked *