PHP User Registration Form

In this tutorial, we are going to see an example for creating new user registration form. In the previous tutorial, while seeing PHP form validation we have seen a registration form example.

In this example, we are validating registration form input validation. And then, we are adding form data to the user table after successful validation process.

View DemoDownload


HTML Registration Form

The HTML code for the user registration form is.

<form name="frmRegistration" method="post" action="">
	<table border="0" width="500" align="center" class="demo-table">
		<td><input type="text" class="demoInputBox" name="userName" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>
		<tr><td>First Name</td>
		<td><input type="text" class="demoInputBox" name="firstName" value="<?php if(isset($_POST['firstName'])) echo $_POST['firstName']; ?>"></td>
		<tr><td>Last Name</td>
		<td><input type="text" class="demoInputBox" name="lastName" value="<?php if(isset($_POST['lastName'])) echo $_POST['lastName']; ?>"></td>
		<td><input type="password" class="demoInputBox" name="password" value=""></td>
		<tr><td>Confirm Password</td>
		<td><input type="password" class="demoInputBox" name="confirm_password" value=""></td>
		<td><input type="text" class="demoInputBox" name="userEmail" value="<?php if(isset($_POST['userEmail'])) echo $_POST['userEmail']; ?>"></td>
		<td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php  } ?>> Male
		<input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php  } ?>> Female
		<td><input type="checkbox" name="terms"> I accept Terms and Conditions</td>
	<div><input type="submit" name="submit" value="Register" class="btnRegister"></div>

And the styles are,

.message {color: #FF0000;font-weight: bold;text-align: center;width: 100%;padding: 10;}
.demo-table {background:#FFDFDF;width: 100%;border-spacing: initial;margin: 20px 0px;word-break: break-word;table-layout: auto;line-height:1.8em;color:#333;}
.demo-table td {padding: 20px 15px 10px 15px;}
.demoInputBox {padding: 7px;border: #F0F0F0 1px solid;border-radius: 4px;}
.btnRegister {padding: 10px;background-color: #09F;border: 0;color: #FFF;cursor: pointer;}

PHP Registration Form Input Validation

This PHP code is used for validating registration form inputs in server side.

/* Form Required Field Validation */
foreach($_POST as $key=>$value) {
if(empty($_POST[$key])) {
$message = ucwords($key) . " field is required";
/* Password Matching Validation */
if($_POST['password'] != $_POST['confirm_password']){ 
$message = 'Passwords should be same<br>'; 

/* Email Validation */
if(!isset($message)) {
if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL)) {
$message = "Invalid UserEmail";

/* Validation to check if gender is selected */
if(!isset($message)) {
if(!isset($_POST["gender"])) {
$message = " Gender field is required";

/* Validation to check if Terms and Conditions are accepted */
if(!isset($message)) {
if(!isset($_POST["terms"])) {
$message = "Accept Terms and conditions before submit";

Add Registered User Data to Database

After successful validation, we are executing the insert query for adding registered user information to the database.

$db_handle = new DBController();
$query = "INSERT INTO registered_users (user_name, first_name, last_name, password, email, gender) VALUES
('" . $_POST["userName"] . "', '" . $_POST["firstName"] . "', '" . $_POST["lastName"] . "', '" . md5($_POST["password"]) . "', '" . $_POST["userEmail"] . "', '" . $_POST["gender"] . "')";
$result = $db_handle->insertQuery($query);
if(!empty($result)) {
	$message = "You have registered successfully!";	
} else {
	$message = "Problem in registration. Try Again!";	

View DemoDownload

This PHP code tutorial was published on November 22, 2014.

↑ Back to Top