Registration Module Code
Step 1 :
create table studentrecord
(id int primary key auto_increment,
name varchar(20),
email varchar(20),
phone varchar(20),
password varchar(20));
(1)step one write code for file "registration.html":
<html>
<head>
<script>
function ajax_post()
{
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "validation.php";
var fn = document.getElementById("name").value;
var en = document.getElementById("email").value;
var pn = document.getElementById("phone").value;
var pd = document.getElementById("password").value;
var cpd = document.getElementById("cpassword").value;
var cap=document.getElementById("captcha").value;
var vars = "name="+fn+"&email="+en+"&phone="+pn+"&password="+pd+"&cpassword="+cpd+"&captcha="+cap;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax with PHP examples</h2>
Name:<input id="name" name="name" type="text"><br><br>
EMAIL:<input id="email" name="email" type="text"><br><br>
PHONE<input id="phone" name="phone" type="text"><br><br>
PASSWORD<input id="password" name="password" type="text"><br><br>
cPASSWORD<input id="cpassword" name="cpassword" type="text"><br><br>
(2+ 2)answer<input id="captcha" name="captcha" type="text"><br><br>
<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>
output:
(2)Now write code for "validation.php " :
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$password=$_POST['password'];
$cpassword=$_POST['cpassword'];
$capcha=$_POST['captcha'];
$con=mysql_connect("localhost","root","");
mysql_select_db("aniket",$con);
//include("connect.php");
$sql="select * from studentrecord where email='$email'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";
$errormsg=array();
if($capcha=="" )
{
$errormsg="Plase Enter Your Answer";
}
else if($capcha==4)
{
$errormsg="";
}
else
{
$errormsg="incorrect answer";
}
if($password!==$cpassword)
{
$errormsg="password did not match";
}
if($password=="")
{
$errormsg="enter your password ";
}
if($phone=="")
{
$errormsg="enter your phone ";
}
if(!preg_match( $regex, $email ))
{
$errormsg="enter your valid email";
}
if($email=="")
{
$errormsg="enter your email ";
}
if($name=="")
{
$errormsg="enter your Name ";
}
if(empty($errormsg))
{
if($num>0)
{
echo "<font color=red>email id is already used please use another email id</font>";
}
else
{
$sql1="insert into studentrecord(name,email,phone,password)values('$name','$email','$phone','$password')";
mysql_query($sql1);
echo "<font color=red>successfully registered </font>";
}
}
else
{
echo"<font color=red> $errormsg </font>";
}
?>
==============================================================================================================================================
Another example:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr =$commentErr= "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$websiteErr = "enter your website url";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$commentErr = "enter your comment";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(empty($emailErr) && empty($nameErr) && empty($genderErr )&& empty($websiteErr))
{
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
}
?>
</body>
</html>
Another example:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr =$commentErr= "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$websiteErr = "enter your website url";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$commentErr = "enter your comment";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(empty($emailErr) && empty($nameErr) && empty($genderErr )&& empty($websiteErr))
{
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
}
?>
</body>
</html>
============Registration using php for email ,contact ,fullname =====================
(1)write code for userregistration.html file:
<html>
<head>
<title>
PHP FORM VALIDATION BY OM SIR
</title>
<link rel="stylesheet" href="bootstrap.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function (event) {
// triggered when the form submitted
$("#myform").on('submit',(function(event) {
event.preventDefault();
$("#response").empty();
$.ajax({
// URL to move the uploaded image file to server
url: "submit.php",
// Request type
type: "POST",
// To send the full form data
data: new FormData(this),
contentType: false,
processData:false,
// UI response after the file upload
success: function(data)
{
$("#response").html(data);
}
});
}));
// Triggered when the image changed (browse)
// Function to show the image as a preview
});
</script>
</head>
<body>
<div class="container">
<img src="otts.png" style="min-wdith:300px; width:100%; height:200px;">
<h1>If you are already a Registered User, Please Sign In with your Email & Password</h1>
<div class="form-group">
<a href="userlogin.html"><input type="submit" name="submit" Value="Sign-In" class="btn-lg btn-primary"></a>
</div>
<h1>New User Registration For OTTS!</h1>
<div id=response> </div>
<form id=myform action="" method="post" >
<div class="form-group">
Name <input type="text" name="name" class="form-control">
</div>
<div class="form-group">
Contact<input type="text" name="contact" class="form-control">
</div>
<div class="form-group">
Email<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
Password<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn-lg btn-primary">
</div>
</form>
</div>
</body>
</html>
(2)WRITE CODE FOR "submit.php" file:
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$password=$_POST['password'];
$con=mysqli_connect("localhost","root","password","databasename");
$sql="select * from userrecord where email='$email'";
$result=mysqli_query($con,$sql);
$num=mysqli_num_rows($result);
$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";
$errormsg=array();
if(!preg_match( $regex, $email ))
{
$errormsg="enter your valid email";
}
if($password=="")
{
$errormsg="enter your password ";
}
if(!preg_match('/^[0-9]{10}+$/', $_POST['contact']))
{
$errormsg = 'Invalid Number,Please enter 10 digit mobile number !';
}
if($email=="")
{
$errormsg="enter your email ";
}
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errormsg = "Only letters and white space allowed";
}
if (empty($_POST["name"]))
{
$errormsg = "Name is required";
}
if(empty($errormsg))
{
if($num>0)
{
echo "<font color=red>email id is already used please use another email id</font>";
}
else
{
$sql1="insert into userrecord(name,email,contact,password)values('$name','$email','$contact','$password')";
mysqli_query($con,$sql1);
echo "<font color=red>successfully registered </font>";
}
}
else
{
echo"<font color=red> $errormsg </font>";
}
?>
(1)write code for userregistration.html file:
<html>
<head>
<title>
PHP FORM VALIDATION BY OM SIR
</title>
<link rel="stylesheet" href="bootstrap.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function (event) {
// triggered when the form submitted
$("#myform").on('submit',(function(event) {
event.preventDefault();
$("#response").empty();
$.ajax({
// URL to move the uploaded image file to server
url: "submit.php",
// Request type
type: "POST",
// To send the full form data
data: new FormData(this),
contentType: false,
processData:false,
// UI response after the file upload
success: function(data)
{
$("#response").html(data);
}
});
}));
// Triggered when the image changed (browse)
// Function to show the image as a preview
});
</script>
</head>
<body>
<div class="container">
<img src="otts.png" style="min-wdith:300px; width:100%; height:200px;">
<h1>If you are already a Registered User, Please Sign In with your Email & Password</h1>
<div class="form-group">
<a href="userlogin.html"><input type="submit" name="submit" Value="Sign-In" class="btn-lg btn-primary"></a>
</div>
<h1>New User Registration For OTTS!</h1>
<div id=response> </div>
<form id=myform action="" method="post" >
<div class="form-group">
Name <input type="text" name="name" class="form-control">
</div>
<div class="form-group">
Contact<input type="text" name="contact" class="form-control">
</div>
<div class="form-group">
Email<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
Password<input type="password" name="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn-lg btn-primary">
</div>
</form>
</div>
</body>
</html>
(2)WRITE CODE FOR "submit.php" file:
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$password=$_POST['password'];
$con=mysqli_connect("localhost","root","password","databasename");
$sql="select * from userrecord where email='$email'";
$result=mysqli_query($con,$sql);
$num=mysqli_num_rows($result);
$regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$^";
$errormsg=array();
if(!preg_match( $regex, $email ))
{
$errormsg="enter your valid email";
}
if($password=="")
{
$errormsg="enter your password ";
}
if(!preg_match('/^[0-9]{10}+$/', $_POST['contact']))
{
$errormsg = 'Invalid Number,Please enter 10 digit mobile number !';
}
if($email=="")
{
$errormsg="enter your email ";
}
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errormsg = "Only letters and white space allowed";
}
if (empty($_POST["name"]))
{
$errormsg = "Name is required";
}
if(empty($errormsg))
{
if($num>0)
{
echo "<font color=red>email id is already used please use another email id</font>";
}
else
{
$sql1="insert into userrecord(name,email,contact,password)values('$name','$email','$contact','$password')";
mysqli_query($con,$sql1);
echo "<font color=red>successfully registered </font>";
}
}
else
{
echo"<font color=red> $errormsg </font>";
}
?>
Comments
Post a Comment