simple registration form in php with validation
![]() |
| simple registration form in php with validation |
Hi Friends Today we will learn how to create a simple registration form in php MySQL with validation...
just follow the examples to learn how to create simple registration form with validation in php mysql..
=>Database and Table:
Database Name: test
Table Name: user
Table Structure for Table user
CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `password` varchar(50) NOT NULL, `email` varchar(20) NOT NULL, `sex` varchar(10) NOT NULL, `country` varchar(20) NOT NULL, `fullname` varchar(20) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Copy the Below codes
index.php
<?php
$conn=new mysqli('localhost','root','','test');
if(isset($_POST['submit'])){
//UserName Checking
if(empty($_POST['name'])){
$nameerror='Your UserName is Empty';
$error='';
}
//Email Checking and Validation Start
if(!empty($_POST['email'])){
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$emailerror='<font color=green>'.$_POST['email'].'</font> is Not Valid';
$error='';
}
}else{
$emailerror='Your Email is Empty';
$error='';
}
//Email Validation End
//Password Validation
if(!empty($_POST['password'])){
if(strlen($_POST['password'])<8){
$passerror='Your Password Should not be less Than 8 Characters';
$error='';
}
}else{
$passerror='Your Password is Empty';
$error='';
}
//Full Name Validation
if(empty($_POST['fname'])){
$fnameerror="Your Full Name Can Not be Empty";
$error='';
}
//Sex Checking
if(empty($_POST['sex'])){
$sexerror="Your Sex Can Not be Empty";
$error='';
}
//Country Checking
if(empty($_POST['country'])){
$countryerror="Your Country Can Not be Empty";
$error='';
}
if(!isset($error)){
//Inserting User Info Into Database
$username=mysqli_real_escape_string($conn,$_POST['name']);
$password=mysqli_real_escape_string($conn,$_POST['password']);
$email=mysqli_real_escape_string($conn,$_POST['email']);
$fname=mysqli_real_escape_string($conn,$_POST['fname']);
$sex=mysqli_real_escape_string($conn,$_POST['sex']);
$country=mysqli_real_escape_string($conn,$_POST['country']);
$result=mysqli_query($conn,"INSERT INTO user(username,password,email,sex,country,fullname)
VALUES('$username','$password','$email','$sex','$country','$fname')");
if($result){
$success="Thank You.Your Registration Successfull";
}else{
$error="Sorry Something went Wrong.Please Try again";
}
}
}
?>
<style>
.error{
color:red;
font-weight:bold;
font-style:italic;
}
input[type=text]{
padding:5px;
border-radius:4px;
}
input[type=submit]{
padding:7px;
background:black;
color:white;
border-radius:4px;
}
input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #d1d3d1;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: #ffa500;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid white;
}
select {
padding:3px;
margin: 0;
font-size:15px;
width:185px;
border-radius:4px;
background: white;
color:#888;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
cursor:pointer;
}
</style>
<div align="center">
<h3>PHP MySQL Registration System</h3>
<form method="POST">
<table width="50%">
<tr>
<td>UserName:</td>
<td><input type="text" name="name"/>
<div><?php if(isset($nameerror)){
echo '<div class=error>'.$nameerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"/>
<div><?php if(isset($emailerror)){
echo '<div class=error>'.$emailerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"/>
<div><?php if(isset($passerror)){
echo '<div class=error>'.$passerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td>Full Name:</td>
<td><input type="text" name="fname"/>
<div><?php if(isset($fnameerror)){
echo '<div class=error>'.$fnameerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td>Sex:</td>
<td><input type="radio" name="sex" value="female"/>Female
<input type="radio" name="sex" value="male"/> Male
<input type="radio" name="sex" value="other"/>Other
<div><?php if(isset($sexerror)){
echo '<div class=error>'.$sexerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select type="text" name="country">
<option value="">Select Country</option>
<option value="india">India</option>
<option value="usa">United States</option>
<option value="japan">Japan</option>
<option value="russia">Russia</option>
</select>
<div><?php if(isset($countryerror)){
echo '<div class=error>'.$countryerror.'</div>';}?></div>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit"/></td>
</tr>
</table>
</form>
<?php if(isset($success)){
echo $success;}?>
<?php if(isset($error)){
echo $error;}?>
</div>
That's it Guys how to create simple registration form in php with validation..Keep Visiting
Thank You...


No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.