2016-10-13 54 views
1

我已經編寫了php代碼來選擇特定的患者信息,我用pdo連接數據庫,文件的結果是json格式。如何從php文件中獲取json數據到Intel XDK

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

但是當我在英特爾XDK寫js代碼來顯示JSON數據,沒有上screen.How顯示的輸出可以我顯示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> 

回答

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響應,如果可能的話貼在這裏所以幫助更多。

+0

它仍然不起作用 – Malak

+0

是反應進入螢火蟲?如果你可以在這裏粘貼這個響應...並嘗試一次下面的代碼,這是jQuery的基礎.. function requestPatientInfo(){ var id = $('#pid')。val(); $阿賈克斯({ 的URL:http://localhost/hospital/GetPatientData2.php, 數據:{ID:ID}, 數據類型:JSON, },成功:功能(數據){ $('# divPatientInfo')。html(data.message); }); } 確保包含jquery庫http://code.jquery.com/ –