下面是index.html文件<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無刷新留言簿</title><style type="text/css">
body {
text-align:center;
font-family:"宋體",Arial;
margin:0;
padding:0;
backgroud:#FFF;
font-size:12px;
color:#6D6D6D;
background-color:#F0F0F0;
}
div,form,img,ul,ol,li,dl,dt,dd,p {
margin:0;
padding:0;
border:0;
}
li{
list-style:none;
margin-bottom:5px;
}
input,textarea {
border:1px solid #CCCCCC;
overflow:hidden;
}
.sub{
height:20px;
width:48px;
bakground:url(input_bg.gif);
border:0;
padding:2px 0 0;
font-size:12px;
}
.purple,a.purple:like,a.purple:visited{
color:#AC349B
}
.cBlack,a.cBlack:link,a.cBlack:visited{
color:#000
}
#head {
margin:5px 0 8px 0;
font:bold 12px Arial,Helvetica,sans-serif;
color:#AC349B;
}
#content{
text-align:left;
margin:2 20px 12px 20px;
padding:8px;
border:1px solid #CBCBCB;
background-color:#FFF;
line-heigth:18px;
}
#revBox {
text-align:left;
margin:-3px 20px 12px 20px;
padding:8px;
border:1px solid #CBCBCB;
background-color:#FFF;
line-height:18px;
}
#list{
text-align:left;
margin:-3px 20px 12px 20px;
}
#list div {
margin-bottom:5px;
padding:8px;
border:1px solid #CBCBCB;
background-color:#FFF;
line-height:18px;
}
.title{
text-align:left;
margin:0 20px 5px 22px;
color:#AC349B;
font-weight:bold;
}</style><script type="text/javascript">
var xmlHttp;
var addNew;
function showre() 
{
obj = document.getElementById("list");
obj.innerHTML = "<div>評論載入中...</div>"; xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.onreadystatechange = showlist;
xmlHttp.open("GET","19-4.php",true); xmlHttp.send(null);
}function showlist()
{
if(xmlHttp.readyState == 4 ) {
if(xmlHttp.status == 200){
document.getElementById("list").innerHTML="";
var xmlAuthor = xmlHttp.responseXML.getElementsByTagName("Author");
var xmlContent = xmlHttp.responseXML.getElementsByTagName("Content"); for(i=0;i<xmlContent.length;i++){
var Content = xmlContent[i].firstChild.data;
Content = Content.split("\n");
var temp="";
for(j=0;j<Content.length;j++) {
if(j==Content.length-1){
temp = temp + Content[j];
}else {
temp = temp + Content[j]+"<br>";
}
}
var div = document.createElement("DIV");
div.id = i;
if(Content == "沒有留言") {
div.innerHTML = "沒有留言";
}else {
var Author = xmlAuthor[i].firstChild.data;
var PostTime = xmlAuthor[i].getAttribute("PostTime");
div.innerHTML = "<span class='purple'>" + Author + "</span>于" + PostTime +"發表留言:<br /><span class='cBlack'>" + temp + "</span>";

}
document.getElementById("list").appendChild(div);
}
}
else {
alert("執行過程中出現問題,服務器返回:"+xmlHttp.statusText);
}
}
}function AddNew()
{
obj = document.getElementById("sendGuest");
obj.disabled = true;
var Author = document.getElementById("memAuthor").value;
var Content = document.getElementById("memContent").value;
if(Author == ""||Content=="") {
alert("請完整填寫!");
obj.disabled = false;
return false;
}
addNew = new ActiveXObject("Microsoft.XMLHTTP");
addNew.onreadystatechange = sendGuest;
var GuestInfo = "Author="+Author + "&Content="+Content;
addNew.open("POST","19-2.php",true);
addNew.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
addNew.send(GuestInfo);
}function sendGuest()
{
if(addNew.readyState == 4) {
if(addNew.status == 200) {
obj = document.getElementById("head");
obj.innerHTML = unescape(addNew.responseText);
document.getElementById("sendGuest").disabled = false;
document.getElementById("memAuthor").value="";
document.getElementById("memContent").value="";
showre();
}
else {
alert("執行過程中出現問題,服務器返回:"+addNew.statusText);
}
}
}function reset()
{
obj1 = document.getElementById("memAuthor");
    obj2 = document.getElementById("memContent");
    obj1.value = "";
    obj2.value = "";
}
</script>
</head>
<body onLoad="showre()">
<div id="head">無刷新留言簿</div>
<div class="title">留言列表</div>
<div id="list"></div>
<div class="title">發表新留言</div>
<div id="revBox">
<ul>
<li><label>昵稱 <input name="memAuthor" type="text"
id="memAuthor" /> </label></li>
<li><label>內容 <textarea name="memContent" cols="50"
rows="5" id="memContent" ></textarea> </label></li>
<li><label> <input type="button" onclick="AddNew()"
value="確認" class="sub" id="sendGuest" /> </label> <label> <input
type="button" value="重置" class="sub" onclick="reset()" /> </label></li>
</ul></div>
</body>
</html>
以下是19-4.php文件<?php
include "19-3.php";
header("Content-Type:text/html;charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<data>";
$sql= "select * from $table order by id desc";
$result = mysql_query($sql,$my_conn);if(mysql_num_rows($result)==0) {
echo "<Content>沒有留言</Content>";
}else {
while($rows=mysql_fetch_array($result)){
echo "<Author PostTime=\"".$rows[do_time]."\">".$rows[author]."</Author>";
echo "<Content>".$rows[content]."</Content>";
}
}
echo "</data>";?>问题是:为什么获取不到19-4.php中输出的xml数据,数据库中是有数据的,就算没数据也应该显示“没有留言”啊!但是什么都不显示。我把 xmlHttp.open("GET","19-4.php",true);这断代码中的19-4.php换成一个其他xml文件,是可以获得数据的。求帮助啊!

解决方案 »

  1.   

    include "19-3.php";
    header("Content-Type:text/html;charset=utf-8");
    一种可能性是"19-3.php"有输出,执行header出错,后面的代码都没有执行。
      

  2.   

    不是这个问题,我可以把19-4.php输出的数据写入一个xml文件,并被javascript获取
      

  3.   

    <?php
    header("Content-Type:text/xml;charset=utf-8");
    include "19-3.php";
    也许是浏览器预计的数据类型就是HTML,IE有这毛病
      

  4.   

    会不会是那个名称起的有问题啊,改下试试19to3.php
      

  5.   

    xmlHttp.open("GET","19-4.php",true);改成xmlHttp.open("POST","19-4.php",true);
    试试
    用GET方式可能会拿不到最新的信息