You are here: Home » PHP » PHP Registration Form

PHP Registration Form

16 Flares 16 Flares ×

Step 1
create a html registration form.
Simple php registration for, just copy below code and paste it into body part of a php page.

<form action="" name="regiform" method="post">
<table border="1">
<tr><th>Name</th><td><input type="text" name="name" id="name"  onblur="removes(this.id);"/></td></tr>
<tr><th>Email</th><td><input type="text" name="email" id="email" onblur="removes(this.id);" /></td></tr>
<tr><th>Password</th><td><input type="password" name="pass" id="pass" onblur="removes(this.id);" /></td></tr>
<tr><th>Re-Password</th><td><input type="password" name="repass" id="repass" onblur="removes(this.id);" /></td></tr>
<tr><th>I am</th><td>
<select name="gender" id="gender" onblur="removes(this.id);">
<option value="">Gender</option>
<option value="male">male</option>
<option value="female">Female</option>
</select>
</td>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" onclick="return validate();" /></td></tr>
</table>
</form>

In above code, i am calling a function validate(), for validation.
Step 2
we use java script form validation. Use below code and paste it in head part of a php page.

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

<script>
function validate()
{
if(document.getElementById("name").value=='')
{
	 $("#name").css({"border": "1px solid red"});
	 document.regiform.name.focus();
     return false;
}
if(document.getElementById("email").value=='')
{
	 $("#email").css({"border": "1px solid red"});
	 document.regiform.email.focus();
     return false;
}

if(document.getElementById("email").value!='')
{
var em =document.getElementById('email').value;


if (echeck(em)==false){
		document.regiform.email.value="";
		document.regiform.email.focus();
		return false;


}
	}

if(document.getElementById("pass").value=='')
{
	 $("#pass").css({"border": "1px solid red"});
	 document.regiform.pass.focus();
     return false;
}

if(document.getElementById("repass").value=='')
{
	 $("#repass").css({"border": "1px solid red"});
	 document.regiform.repass.focus();
     return false;
}

if((document.getElementById("pass").value)!=(document.getElementById("repass").value))
{
	 $("#repass").css({"border": "1px solid red"});
	 document.regiform.repass.focus();
     return false;
}

if(document.getElementById("gender").value=='')
{
	 $("#gender").css({"border": "1px solid red"});
	 document.regiform.gender.focus();
     return false;
}

}
function echeck(str) {
var emailText = str;
    var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
    if (pattern.test(emailText)) {
        
    } else {
       
  return false;
    }

}
function removes(id){
	
 	$("#"+id).css({"border": "1px solid #BFBFBF"}); 
 }
</script>
</head>

Step 3
Create a datebase, and table

CREATE DATABASE `mydatabase` ;
CREATE TABLE `mydatabase`.`registration` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 55 ) NOT NULL ,
`email` VARCHAR( 55 ) NOT NULL ,
`pass` VARCHAR( 255 ) NOT NULL ,
`gender` VARCHAR( 10 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

Step 4
Create datebase connection

<?php
$user="newindia";
$pass="123456";
$database="mydatabase";
$link=mysql_connect("localhost",$user,$pass) or die("Mysql not  connected");
$db=mysql_select_db($database,$link) or die("Database not connected");
?>

Step 5
Now i am going to insert data, after form submit. After submit, first check all require fiels are not blank through
PHP, it is also called Server Side Validation, After it write insert query.

if($_POST['submit']!='')
{

$name=$_POST['name'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$gender=$_POST['gender'];

if(($name!='') &&  ($email!='') && ($pass!='') && ($gender!=''))
{
$rr=mysql_query("insert into registration set name='".$name."',email='".$email."',pass='".$pass."',gender='".$gender."'  ");
header('location:registration.php?st=1');
exit();
}
else
{
header('location:registration.php?st=2');
exit();

}
}

If all require fields are blank, redirect page with st=2, and display error message.
On the othehand in all require filled are filled, after insert query, redirect page with st=1 and show Thank you message.

About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

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

*

Scroll To Top
16 Flares Twitter 5 Facebook 2 Google+ 2 StumbleUpon 4 Pin It Share 0 LinkedIn 3 16 Flares ×