Naren Verma
Naren Verma

Reputation: 2307

How to check email id is existing or not using ajax in php?

I have to check email id is existing or not. I know this question is ask server time and I also checked on google but still getting the issue. I have tried some code but I don't know where I am wrong. Would you help me in this?

  <input type="email" name="email" id="email">
  <div id="status"></div>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
     $("#email").keyup(function() {
        var name = $('#email').val();
        if(name=="")
        {
            $("#status").html("");
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "process.php",
                data: "email="+ name ,
                success: function(html){
                    $("#status").html(html);
                }
            });
            return false;
        }
    });
});
</script>

Process.php

/*Checking email available or not*/
if(isset($_POST['email']))
{
    $chk_email=$_POST['email'];
    $sql=" SELECT Email FROM request WHERE Email='$chk_email'";
    $result_email = mysqli_fetch_assoc($conn->query($sql));

    if($result_email==0){
        echo "<span style='color:green;'>Available</span>";         
    }
    else
    {
        echo "<span style='color:red;'>Already register</span>";    
    }
}

Upvotes: 0

Views: 94

Answers (1)

Zeus
Zeus

Reputation: 1275

You need to post data like this :

$.ajax({
    type: "POST",
    url: "process.php",
    data: {email: name},//<--- Look here
    success: function(html){
        $("#status").html(html);
    }
});

Oh and in your php code use mysqli_escape_string to escape the strings to avoid mysql injections.

Upvotes: 1

Related Questions