(1) see Ajax code which passes data from form to php , see code of Registrationform.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("capcha").value;
var vars = "name="+fn+"&email="+en+"&phone="+pn+"&password="+pd+"&cpassword="+cpd+"&capcha="+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>
<div id="status"></div>
Name <input type="text" id="name" name="name" ><br></br>
email <input type="text" id="email" name="email" ><br></br>
phone <input type="text" id="phone" name="phone" ><br></br>
password<input type="text" id="password" name="password" ><br></br>
confirm password<input type="text" id="cpassword" name="cpassword" ><br></br>
2+2=<input type="text" id="capcha" name="name" ><br></br>
<input name="submit" id="Button1" type="submit" value="Submit Data" onclick="ajax_post();" onclick="alert(ValidCaptcha());">
</body>
</html>
(2) write code for form validation in php ,see "validation.php file code:"
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$password=$_POST['password'];
$cpassword=$_POST['cpassword'];
$capcha=$_POST['capcha'];
$con=mysql_connect("localhost","root","");
mysql_select_db("itexam",$con);
$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($name=="")
{
$errormsg="enter your Name ";
}
if($email=="")
{
$errormsg="enter your email ";
}
if($phone=="")
{
$errormsg="enter your phone ";
}
if($password=="")
{
$errormsg="enter your password ";
}
if(!preg_match( $regex, $email ))
{
$errormsg="enter your valid email";
}
if($password!==$cpassword)
{
$errormsg="password did not match";
}
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>";
}
?>
<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("capcha").value;
var vars = "name="+fn+"&email="+en+"&phone="+pn+"&password="+pd+"&cpassword="+cpd+"&capcha="+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>
<div id="status"></div>
Name <input type="text" id="name" name="name" ><br></br>
email <input type="text" id="email" name="email" ><br></br>
phone <input type="text" id="phone" name="phone" ><br></br>
password<input type="text" id="password" name="password" ><br></br>
confirm password<input type="text" id="cpassword" name="cpassword" ><br></br>
2+2=<input type="text" id="capcha" name="name" ><br></br>
<input name="submit" id="Button1" type="submit" value="Submit Data" onclick="ajax_post();" onclick="alert(ValidCaptcha());">
</body>
</html>
(2) write code for form validation in php ,see "validation.php file code:"
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$password=$_POST['password'];
$cpassword=$_POST['cpassword'];
$capcha=$_POST['capcha'];
$con=mysql_connect("localhost","root","");
mysql_select_db("itexam",$con);
$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($name=="")
{
$errormsg="enter your Name ";
}
if($email=="")
{
$errormsg="enter your email ";
}
if($phone=="")
{
$errormsg="enter your phone ";
}
if($password=="")
{
$errormsg="enter your password ";
}
if(!preg_match( $regex, $email ))
{
$errormsg="enter your valid email";
}
if($password!==$cpassword)
{
$errormsg="password did not match";
}
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>";
}
?>
No comments:
Post a Comment