[英]Javascript error when using AJAX
I am trying to do a connection with the database using AJAX. 我正在尝试使用AJAX与数据库建立连接。 I am totally new to AJAX...just saw a couple of tutorials and took a code snippet and tried to adapt it to my code.. but when ever I press the button I get the following error Error: Unable to get property 'documentElement' of undefined or null reference 我对AJAX完全陌生...只是看了几本教程,并获取了一个代码片段,并试图使其适应我的代码..但是,每当我按下按钮时,我都会收到以下错误消息:无法获取属性'documentElement的未定义或空引用
php file 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 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 !');
}
}
}
the form containing the button which calls the javascript 包含调用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] One more thing, see the 1]还有一件事,请参阅
<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?>
as value
of each <option>
is same. 因为每个<option>
value
都相同。 You will get same value
of title
at 您将在获得相同的title
value
title= encodeURIComponent(document.getElementById("title").value);
so no matter what option you select, you will always get same value ie \\mesazhi\\
. 因此,无论您选择什么选项,都将始终获得相同的值,即\\mesazhi\\
。
2] Also, take a look, 2]另外,看看
$sql="select patient_text, doctor_text where title='".$title."')";
you have not mention the table name. 您还没有提及表格名称。
3] Also why is )
at the end of query? 3]为什么在查询末尾也有)
?
4] And finally 4]最后
$conn=("localhost","root","","askthedoctor");
should be 应该
$conn=mysqli_connect("localhost","root","","askthedoctor");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.