donglikuang8145 2016-03-13 19:52
浏览 35

阿贾克斯。 从查询中检索信息

I am really confused at the moment, I am very new to all this just being learning java script and php . I am trying to use ajax to check the db to see if the email exist and then if it is cancel the submit of the form. I cant seem to retrieve the information from the XML. I am probably doing it completely wrong, but that why I am asking here , to lean

So if you could help would be great

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
    $status;
    
if (isset($_GET['email'])) {
    $email_in_use = $_GET['email'];

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

    if(mysqli_num_rows($query) > 0){

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

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

</div>
  • 写回答

1条回答 默认 最新

  • dongpu8935 2016-03-13 20:32
    关注

    You are building and handling ajax XMLHttpRequest in an incorrect way.
    Also, to be able to receive an XML response - set additional request header(will be shown further).
    Change you ajax request as shown below:

       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);
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c