http://beski.wordpress.com/2009/05/16/check-email-already-exist-ajax-jquery/

*************


In a registration form i needed to check whether the email already exist or not.

For this i have tried with jquery ajax, we can use this to check usernames, emails,url names… etc.
THE DEMO
How i did…

HTML

Email

<input type=”text” name=”email” id=”email” size=”20″ class=”input_s1_normal”>

<div id=”emailInfo” align=”left”>



Script
First declare a flag variable and other necessary variables
var emailok = false;
var email = $(“#email”);

Send Ajax request to check.php
email.blur(function(){
$.ajax({
type: “POST”,
data: “email=”+$(this).attr(“value”),
url: “check.php”,
beforeSend: function(){
emailInfo.html(“Checking Email…”); //show checking or loading image
},

success: function(data){
if(data == “invalid”)
{
emailok = false;
emailInfo.html(“Inavlid Email”);
}
else if(data != “0″)
{
emailok = false;
emailInfo.html(“Email Already Exist”);
}
else
{
emailok = true;
emailInfo.html(“Email OK”);
}
}
});
});

PHP
This is the check.php file
//data connection file
require “config.php”;
extract($_REQUEST);

if(eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email))
{
$sql = “select * from users where email=’$email’”;
$rsd = mysql_query($sql);
$msg = mysql_num_rows($rsd);
//returns 0 if email not already exist
s
}
else
{
$msg = “invalid”;
}
echo $msg;
?>

Script
Process the response
email.blur(function(){
$.ajax({
type: “POST”,
data: “email=”+$(this).attr(“value”),
url: “check.php”,
beforeSend: function(){
emailInfo.html(“Checking Email…”);
},
success: function(data){
if(data == “invalid”)
{
emailok = false;
emailInfo.html(“Inavlid Email”);
}
else if(data != “0″)
{
emailok = false;
emailInfo.html(“Email Already Exist”);
}
else
{
emailok = true;
emailInfo.html(“Email OK”);
}
}

});
});

arrow
arrow
    全站熱搜