2016-03-13 150 views
0

我现在真的很困惑,我对所有这些只是学习java脚本和php都很陌生。我正在尝试使用ajax检查数据库以查看电子邮件是否存在,然后是否取消提交表单。我似乎无法从XML中检索信息。我可能这样做完全错误的,但为什么我问这里,瘦ajax。从查询中检索信息

所以,如果你能帮助将是巨大的

JAVA SCRIPT

//validate the sign up/regiser form 
 
function validateForm() { 
 

 
    //Get password varibles 
 
    var pass = document.forms["signup"]["sign-up-password"].value; 
 
    var confPass = document.forms["signup"]["password-confirm"].value; 
 

 
    //Check if they match 
 
    if (pass != confPass) { 
 
     alert("Password does not match"); 
 
     return false; 
 
    } 
 

 
    //Ajax functions 
 
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4){ 
 

 
    alert("im here"); 
 

 
    email = document.getElementById('email2').value; 
 
    xmlHttp.open("GET", "php/ajaxCom.php?email=" + email, true); 
 
    xmlHttp.onreadystatechange = handleServerResponse; 
 

 
    xmlHttp.send(null); 
 

 
    }else{ 
 
    setTimeout('process()',1000); 
 
    } 
 

 
} 
 

 
function handleServerResponse(){ 
 
    if(xmlHttp.readyState==4){ 
 
    var check=xmlHttp.status; 
 
    if(xmlHttp.status==200){ 
 
     alert("also here 2"); 
 
     xmlResponse = xmlHttp.responseXML; 
 
     xmlDocumentElement = xmlResponse.documentElement; 
 
     message = xmlDocumentElement.firstChild.data; 
 

 
     alert(message); 
 
     return message; 
 
    } 
 
    } 
 
}

php/xml

<?php 
 
\t $status; 
 
\t 
 
if (isset($_GET['email'])) { 
 
\t $email_in_use = $_GET['email']; 
 

 
\t $query = mysqli_query($link, "SELECT * FROM users WHERE email='".$email_in_use."'"); 
 

 
\t if(mysqli_num_rows($query) > 0){ 
 

 
\t  $status = false; 
 
\t }else{ 
 
\t  if(!mysqli_query($link, $query)) 
 
\t \t { $status = mysqli_error($link); } 
 
\t \t else 
 
\t \t { $status = true; } 
 
\t } 
 

 
\t $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" standalone="yes" ?><response><status/></response>'); 
 
\t $xml->response->status = $status; 
 
\t echo $xml->asXML(); 
 
\t echo $status; 
 
} 
 
?>

+0

看这句话'mysqli_query($连接,$查询)'。 '$ query'应该已经是一个结果对象。您也可以使用此代码注入SQL注入,查看准备好的语句。 – chris85

+0

@ chris85这是一个Object它是我的查询来检查电子邮件 –

+0

'$ query'虽然不是查询,但它是一个结果对象(或false),因为您已经执行了查询。 – chris85

回答

0

你需要建立在一个不正确的方式处理AJAX XMLHttpRequest
此外,要能够收到XML响应 - 设置附加请求标头(将进一步显示)。
如下图所示 更改你Ajax请求:

var xmlHttp = null; // this variable should be global to access from different functions 
    ... 
    //Ajax functions 

    email = document.getElementById('email2').value; 

    xmlHttp = new XMLHttpRequest();  
    xmlHttp.open("GET", "php/ajaxCom.php?email=" + email, true); 
    xmlHttp.setRequestHeader("Accept", "text/xml"); 
    xmlHttp.onreadystatechange = handleServerResponse; 
    xmlHttp.send(null); 
... 

function handleServerResponse(){ 
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { 
     var check=xmlHttp.status; 
     var xmlResponse = xmlHttp.responseXML; 
     var xmlDocumentElement = xmlResponse.documentElement; 
     message = xmlDocumentElement.firstChild.data; 
     alert(message); 
     return message; 
    } else{ 
    setTimeout('process()',1000); 
    } 
} 
+0

感谢您的回复,但消息出来punting注意 –

+0

你能说我的php xml是错误的 –

+0

尝试评论这行'echo $ status;'发送只xml结构。用'xmlDocumentElement.childNodes [0]'替换这一行'xmlDocumentElement.firstChild.data',并通过'console.log(message)检查'message'变量' – RomanPerekhrest