无法将数据从PHP传递到AJAX

时间:2015-12-20 15:34:14

标签: javascript php jquery html ajax

我在html中有一个输入框。输入通过ajax搜索数据库并在前端返回结果。问题是我没有从PHP获得结果。我不知道我做错了什么,所以我希望你们对我有更好的理解。

HTML

<body onload="AjaxFindPerson()">
.....
</body>

JS

    var xmlHttp = createXmlHttpRequestObject();



function AjaxFindPerson() {
    if ((xmlHttp.readyState == 0 || xmlHttp.readyState == 4) && document.getElementById("PersonSearchInput").value != "") {
        person = encodeURIComponent(document.getElementById("PersonSearchInput").value);
        xmlHttp.open("GET", "../lib/search.php?email=" + person, true);
        xmlHttp.onreadystatechange = handleServerResponse;
        xmlHttp.send(null);

    }
    else {
        document.getElementById('Label-Result').innerHTML = "";
        document.getElementById('UserNameSearchResult').innerHTML = "";
        $('#add-person-btn').attr("disabled", "disabled");
        setTimeout('AjaxFindPerson()', 1000);
    }
}


function handleServerResponse() {
    if (xmlHttp.readyState == 4 ) {
        if (xmlHttp.status == 200) {
            xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;
            result = xmlDocumentElement.firstChild.data;

            if (result[0] != false) {
                document.getElementById('Label-Result').innerHTML = result[1];
                document.getElementById('UserNameSearchResult').innerHTML = result[0];
                $('#add-person-btn').removeAttr("disabled", "disabled");
            }
            else {
                document.getElementById('Label-Result').innerHTML = result[1];
            }

            setTimeout('AjaxFindPerson()', 1000);
        }
        else {
            alert('Somenthing went wrong when tried to get data from server'+ xmlHttp.readyState);
        }
    }
}

PHP

<?php
    header('Content-Type: text/xml');
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
    session_start();
    define("DB_HOST", 'mysql6.000webhost.com');
    define("DB_USER", '');
    define("DB_PASSWORD", '');
    define("DB_DATABSE", '');

    echo '<response>';

    $email = $_GET['email'];

    $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_DATABSE, $conn);
    $sq = mysql_query("SELECT UserEmail FROM Users");

    $UserInfo = array();

    while ($row = mysql_fetch_array($sq, MYSQL_ASSOC)) {
      $UserInfo[] =  $row['UserEmail'];  
    }


    if (in_array($email, $UserInfo)) {
      $result = mysql_query("SELECT UserName FROM Users WHERE UserEmail = '".$email."'");
      $row = mysql_fetch_row($result);
      $returnRes = array($row[0], "We found results"); //row[0] holds the UserN
      echo $returnRes;
    }
    else {
      $returnRes = array(false, "We couldn't find results");
      echo $returnRes;
    }

    echo '</response>';


?>

如果我们单独检查php-xml文件,则会看到以下图像: enter image description here

我是否需要以另一种方式将值传递给xml-php?

PHP更新1 我设法找到了正确返回数据的方法。这是更新'touch'

 header('Content-Type: application/json');

if (in_array($email, $UserInfo)) {
  $result = mysql_query("SELECT UserName FROM Users WHERE UserEmail = '".$email."'");
  $row = mysql_fetch_row($result);
  echo json_encode(array( 'found' => $row[0], 'msg' => "We found results"));
}
else {
  echo json_encode(array( 'found' => null, 'msg' => "We couldn't find results"));
}

现在的问题是如何操纵js文件来处理返回数组。我试了一下,但没效果:

result = xmlDocumentElement.firstChild.data;

            if (result['found'] != null) {
                document.getElementById('Label-Result').innerHTML = result['msg'];
                document.getElementById('UserNameSearchResult').innerHTML = result['found'];
                $('#add-person-btn').removeAttr("disabled");
            }
            else {
                document.getElementById('Label-Result').innerHTML = result['msg'];
            }

**更新2工作JS **

我弄清楚如何从PHP中检索数据。

xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;
            var result = JSON.parse(xmlDocumentElement.firstChild.data);

            if (result['found'] != null) {
                document.getElementById('Label-Result').innerHTML = result['msg'];
                document.getElementById('UserNameSearchResult').innerHTML = result['found'];
                $('#add-person-btn').removeAttr("disabled");
            }
            else {
                document.getElementById('Label-Result').innerHTML = result['msg'];
            }

现在所有代码都在运作!非常感谢你们!

所有人

+1!

2 个答案:

答案 0 :(得分:3)

四件事:

  1. send(null)的使用似乎不对,只是不在其中传递null。
  2. 第二个是超时方法。相反,您使用它的方式,您可以在回调函数中调用它,或者在函数调用时使用字符串来调用它。
  3. 删除属性的用法也是错误的。它当前正在使用set方法,因为您提供了第二个参数。 remove属性方法仅采用属性名称。
  4. 我建议您为application/json设置标头,并使用json_encode()方法返回数据。

答案 1 :(得分:0)

要打印数组,您可以使用json_encode(),也可以以其他方式将数组转换为字符串。