[英]Javascript error when using AJAX
我正在嘗試使用AJAX與數據庫建立連接。 我對AJAX完全陌生...只是看了幾本教程,並獲取了一個代碼片段,並試圖使其適應我的代碼..但是,每當我按下按鈕時,我都會收到以下錯誤消息:無法獲取屬性'documentElement的未定義或空引用
php文件
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$title = $_GET['title'];
$conn=("localhost","root","","askthedoctor");
$sql="select patient_text, doctor_text where title='".$title."')";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo $row[0];
echo $row[1];
echo '</response>';
?>
Java腳本
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp)
alert("Cant create that object !")
else
return xmlHttp;
}
function process(){
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
title= encodeURIComponent(document.getElementById("title").value);
xmlHttp.open("GET", "displaypatientmessage.php?title="+title,true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}else{
setTimeout('process()',1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
////////////////////////////////error////////////////////
xmlDocumentElement = xmlResponse.documentElement;
/////////////////////////////error/////////////////////////
message1 = xmlDocumentElement.firstChild.data;
document.getElementById("question").innerHTML = message1;
message2 = xmlDocumentElement.firstChild.data;
document.getElementById("answer").innerHTML = message2;
setTimeout('process()', 1000);
}else{
alert('Someting went wrong !');
}
}
}
包含調用javascript的按鈕的表單
<table id="table" class="table">
<tr>
<th>Messages</th>
<th>Problem Description</th>
<th>Doctor's Answer</th>
<th></th>
</tr>
<tr>
<th><select id="title">
<?php
$sql="select title from messages where paitient_id=(select id from login where username='".$username."');";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
?>
<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?>
</select>
</th>
<td><textarea rows="17.95" col="100" id ="question" > </textarea></td>
<td><textarea rows="17.95" col="100" id ="answer" readonly> </textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="button" name="openmessage" value="Display Selected Message" onClick="process()"></td>
</tr>
</table>
</form>
1]還有一件事,請參閱
<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?>
因為每個<option>
value
都相同。 您將在獲得相同的title
value
title= encodeURIComponent(document.getElementById("title").value);
因此,無論您選擇什么選項,都將始終獲得相同的值,即\\mesazhi\\
。
2]另外,看看
$sql="select patient_text, doctor_text where title='".$title."')";
您還沒有提及表格名稱。
3]為什么在查詢末尾也有)
?
4]最后
$conn=("localhost","root","","askthedoctor");
應該
$conn=mysqli_connect("localhost","root","","askthedoctor");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.