检查数据库中是否存在电子邮件

时间:2014-02-17 17:02:22

标签: php jquery mysql ajax validation

当用户注册新帐户时,我正在检查数据库中是否存在电子邮件。当我输入数据库中的电子邮件时,“EmailMessage”不会出现。代码位于:

validateemail.js

function validateemail() {
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
     }
    else
     {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            var response = xmlhttp.responseText;
            response = response.replace(/^\s+|\s+$/g,'')

            if(response == 'error'){
                document.getElementById("EmailMessage").innerHTML='This e-mail address is already taken! If you believe this is in error,  please email <a href="mailto:support@pumpspy.com">support@pumpspy.com</a>';
                document.getElementById("newemail").focus();
                document.getElementById("email_notice").style.display="none";
                document.getElementById("submit_new_account").disabled =true;
            }
            else {
                document.getElementById("EmailMessage").innerHTML="";

            }
        }
      }
    var newemail = document.getElementById("newemail").value;


    if(newemail!=""){
        xmlhttp.open("GET","Checkemail.php?email="+newemail,true);
        xmlhttp.send();
    }

    else{
        document.getElementById("EmailMessage").innerHTML="";

        document.getElementById("submit_new_account").disabled =false;
        }


}

Checkemail.php

    $emailexists = $_GET['email'];

    $sql = executeQuery("SELECT email from tbl_Users WHERE email = '$emailexists'");
    $i = getNumRows($sql);  

     if ($i !=0)    {
         echo "error";          
    }else {
             echo "pass"; 

    }

---



    <input type="text" id="newemail" name="newemail" maxlength="60" size="27" onblur="validateemail();"/>
        </div>
        <div id="EmailMessage" style="color:#F00"></div>

1 个答案:

答案 0 :(得分:0)

尝试:$sql = executeQuery("SELECT email from tbl_Users WHERE email = '".$emailexists."'");

另外,请考虑完全更改代码(问题,安全问题)......