如何从php文件

时间:2016-10-13 14:38:21

标签: php mysql json intel-xdk

我已经编写了PHP代码来选择特定的患者信息,我使用pdo连接数据库,文件的结果是json格式。

{"success":1,"message":"patient info Available!","patient_info":[{"name":"Nora","gender":"M","email":"NouraSaeed@yahoo.com","Date":"2016-10-17"}]}

但是当我在intel xdk中编写js代码来显示json数据时,屏幕上没有显示输出。如何在xdk中显示成功或消息的值!

<html>
<body>

<?php

require("config.inc.php");

$pid = $_GET["id"];
$query = " SELECT * FROM patient where PatientID=$pid";

try {
    $stmt = $db->prepare($query);
    $result = $stmt->execute();
} catch (PDOException $e) {
    $response["success"] = 0;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));
}

$rows = $stmt->fetchAll();

if ($rows) {
    $response["success"] = 1;
    $response["message"] = "patient info Available!";
    $response["patient_info"] = array();

    foreach ($rows as $row) {
        $post = array();
        $post["name"] = $row["PName"];
        $post["gender"] = $row["Gender"];
        $post["email"] = $row["email"];
        $post["Date"] = $row["BDate"];
        array_push($response["patient_info"], $post);
    }

    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No patient_info Available!";
    die(json_encode($response));
}
?>
</body>
</html>

此xdk代码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <script type="text/javascript">
        function createXHR() {
            if (typeof XMLHttpRequest != "undefined")
                return new XMLHttpRequest();
            else
                return new ActiveXObject("Microsoft.XMLHttp");
        }

        function requestPatientInfo() {
            var pid = document.getElementById("pid").value;
            var oXmlHttp = createXHR();
            oXmlHttp.open("get", "http://localhost/hospital/GetPatientData2.php?id=" + pid);
            oXmlHttp.onload = function () {
                if (oXmlHttp.readyState == 4) {
                    if (oXmlHttp.status == 200 || oXmlHttp.status == 304) {
                        var patients = json.parse(oXmlHttp.responseText);
                        divPatientInfo.innerHTML = patients.message;
                    }
                    else
                        divPatientInfo.innerHTML = oXmlHttp.statusText;
                }//end if 
            }; //end ready state
            oXmlHttp.send(null);
        }//end function requestPatientInfo
    </script>
</head>

<body>
Enter Patient ID to get All Information
<p>
    Patient ID: <input type="number" id="pid"/>
</p>
<input type="button" value="Get Info" onClick="requestPatientInfo();"/>
<div id="divPatientInfo"></div>
<iframe name="hiddenFrame" style="width:0 ; height:0"></iframe>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

看起来你还没有定义divPatientInfo ..改变如下

var divPatientInfo = document.getElementById('divPatientInfo'); 
    if(oXmlHttp.readyState==4){
                      if(oXmlHttp.status==200 || oXmlHttp.status== 304 ){
                           var patients= json.parse(oXmlHttp.responseText);  
                           divPatientInfo.innerHTML=patients.message;          
                      }
                       else 
                         divPatientInfo.innerHTML=oXmlHttp.statusText;

                   }//end if

如果没有解决,请使用firebug检查一次ajax响应,如果可能,请在此处粘贴以便更多帮助。