我的三个文件如下
html
<html>
<head>
<script src="responsexml.js"></script>
</head>
<body><form name="for1" id="for1">
Select a User:
<select name="user" id="user">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="button" value="提交" onclick="showuser()">
</form>
<div id="mg"></div>
<span id="name"></span><span id="content"></span><span id="time"></span>
</h2>
</body>
</html>
php文件的代码如下: <?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
// $q=null;
$q=$_GET["q"];
//if(!$q)$q=3;$con = mysql_connect('localhost', 'root', '*********');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db('php', $con);$sql="SELECT * FROM message WHERE id = ".$q."";$result = mysql_query($sql);echo '<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<person>';
while($row = mysql_fetch_array($result))
{
echo "<name>" . $row['name'] . "</name>";
echo "<content>" . $row['content'] . "</content>";
echo "<tiem>" . $row['dat'] . "</time>";
}
echo "</person>";//mysql_close($con);
?>js代码如下:
var xmlHttp;
function showuser()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var str=document.for1.user.options[document.for1.user.selectedIndex].text;
var url="responsexml.php";
url=url+"?q="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//alert("成功!");
xmlDoc=xmlHttp.responseXML;
//alert(xmlDoc);
document.getElementById("name").innerHTML=
xmlDoc.getElementsByTagName("name")[0].childNodes[0].firstChild.data;
document.getElementById("content").innerHTML=
xmlDoc.getElementsByTagName("content")[0].childNodes[0].nodeValue;
document.getElementById("time").innerHTML=
xmlDoc.getElementsByTagName("time")[0].childNodes[0].nodeValue;
}
}function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}我不知道这是怎么回事,为何我的程序运行不正确,那个html文件无法将后台的xml正确先下出来,那位高手给我看看,谢谢了,我调试过,发送请求都没问题。所有代码我都贴出来了,清帮忙给我调试调试,我弄的php+Ajax都没一次成功,希望高手们给我调试成功,在下感激不尽。
html
<html>
<head>
<script src="responsexml.js"></script>
</head>
<body><form name="for1" id="for1">
Select a User:
<select name="user" id="user">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="button" value="提交" onclick="showuser()">
</form>
<div id="mg"></div>
<span id="name"></span><span id="content"></span><span id="time"></span>
</h2>
</body>
</html>
php文件的代码如下: <?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
// $q=null;
$q=$_GET["q"];
//if(!$q)$q=3;$con = mysql_connect('localhost', 'root', '*********');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db('php', $con);$sql="SELECT * FROM message WHERE id = ".$q."";$result = mysql_query($sql);echo '<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<person>';
while($row = mysql_fetch_array($result))
{
echo "<name>" . $row['name'] . "</name>";
echo "<content>" . $row['content'] . "</content>";
echo "<tiem>" . $row['dat'] . "</time>";
}
echo "</person>";//mysql_close($con);
?>js代码如下:
var xmlHttp;
function showuser()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var str=document.for1.user.options[document.for1.user.selectedIndex].text;
var url="responsexml.php";
url=url+"?q="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//alert("成功!");
xmlDoc=xmlHttp.responseXML;
//alert(xmlDoc);
document.getElementById("name").innerHTML=
xmlDoc.getElementsByTagName("name")[0].childNodes[0].firstChild.data;
document.getElementById("content").innerHTML=
xmlDoc.getElementsByTagName("content")[0].childNodes[0].nodeValue;
document.getElementById("time").innerHTML=
xmlDoc.getElementsByTagName("time")[0].childNodes[0].nodeValue;
}
}function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}我不知道这是怎么回事,为何我的程序运行不正确,那个html文件无法将后台的xml正确先下出来,那位高手给我看看,谢谢了,我调试过,发送请求都没问题。所有代码我都贴出来了,清帮忙给我调试调试,我弄的php+Ajax都没一次成功,希望高手们给我调试成功,在下感激不尽。
<root>
<childNode>
<name>****</name>
<content>****</content>
</childNode>
</root>不过,xml不是主要问题。
{
for(var i = 0; i < ele.childNodes.length; i++)
{
var node = ele.childNodes[i];
if(node.nodeType == 3 && !/\S/.test(node.nodeValue))
{
node.parentNode.removeChild(node);
}
}
return ele;
}