2015-12-31 97 views
0

我正在尝试使用AJAX与数据库建立连接。我完全是新的AJAX ...只是看了几个教程,并采取了代码段,并尝试将其适应我的代码..但是当我按下按钮时,我得到以下错误错误:无法获取属性'documentElement '的未定义或空引用使用AJAX时出现Javascript错误

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>'; 
?> 

使用JavasScript

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> 
+0

的可能的复制[AJAX检索XML响应(http://stackoverflow.com/questions/10732013/ajax-retrieving-xml -response) –

+0

无法在任何地方看到表格,我是否错过了它? – Billy

+0

我认为你的php页面被称为'foodstore.php' – Billy

回答

0

1]一两件事,请参阅

<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?> 

作为各<option>value是一样的。您将在

title= encodeURIComponent(document.getElementById("title").value); 

得到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"); 
+0

感谢您的评论,但这不是我的问题,我有 – enu

相关问题