2016-10-13 2 views
1

Я написал PHP-код для выбора конкретной информации о пациенте, я использовал pdo для подключения к базе данных, результатом является формат json.как получить данные json для Intel XDK из php-файла

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

но когда я писал JS код в интел XDK для отображения данных в формате 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 

Если не разрешен проверки один раз АЯКС ответ с помощью поджигатель, если возможно вставить здесь так что помогите больше.

+0

все еще не работает – Malak

+0

ответ в firebug? если вы можете вставить этот ответ здесь ... и попробуйте один раз следующий код, который является базой jquery. function requestPatientInfo() { var id = $ ('# pid'). val(); $ .ajax ({ URL: HTTP: //localhost/hospital/GetPatientData2.php, данные: {: ID}, DATATYPE: JSON, }, успех: функция (данные) { $ ('# divPatientInfo ') .html (data.message); }); } Убедитесь, что включена библиотека jquery http://code.jquery.com/ –