我在用戶註冊新帳戶時檢查數據庫中是否存在電子郵件。當我輸入數據庫中的電子郵件時,「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:[email protected]">[email protected]</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>
你是否縮小了這個範圍?你能直接用電子郵件地址打電話給PHP,並得到正確的迴應嗎? – andrewsi
當我做 – ChaseC
時我得到「錯誤」所以下一步是'var_dump($ sql)'和'var_dump($ i)'看看裏面有什麼值。可能由於某種原因,你的查詢失敗。 – andrewsi