Home > Backend Development > PHP Tutorial > getElementsByTagName问题求解

getElementsByTagName问题求解

WBOY
Release: 2016-06-23 14:06:20
Original
1030 people have browsed it

<html><head><?php	$username=$_GET['username'];	$username=trim($username);//去掉空格	session_start();	$loginuser=$_SESSION['loginuser'];?><meta http-equiv="content-type" content="text/html;charset=utf-8"/><script src="my.js"></script><script>	window.resizeTo(500,400);	window.setInterval("getMessage()",5000);		function getMessage(){			var myXmlHttpRequest=getXmlHttpObject();		if(myXmlHttpRequest){				var url="GetMessageControl.php";			var data="getter=<?php echo $loginuser; ?>&sender=<?php echo $username; ?>";			myXmlHttpRequest.open("post",url,true);			myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");			//重点。指定处理结果的 函数			myXmlHttpRequest.onreadystatechange=function (){				if(myXmlHttpRequest.readyState==4){					if(myXmlHttpRequest.status==200){						var mesRes=myXmlHttpRequest.responseXML;						var cons=mesRes.getElementsByTagName("con");						var sendTimes=mesRes.getElementsByTagName("sendTime");						window.alert(cons.length);						if(cons.length!=0){							for(var i=0;i<cons.length;i++){								$('mycons').value+="<?php echo $username; ?>对<?php echo $loginuser; ?>说:"+cons[i].childNodes[0].nodeValue+" "+sendTimes[i].childNodes[0].nodeValue; 														}						}					}				}						}			myXmlHttpRequest.send(data);		}		}	function sendMessage(){		var myXmlHttpRequest=getXmlHttpObject();		if(myXmlHttpRequest){			var url="SendMessageControl.php";			//js中使用php数据			var data="con="+$('con').value+"&getter=<?php echo $username; ?>&sender=<?php echo $loginuser; ?>";			window.alert(data);					myXmlHttpRequest.open("post",url,true);			myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");			myXmlHttpRequest.onreadystatechange=function (){				if(myXmlHttpRequest.readyState==4){					if(myXmlHttpRequest.status==200){											}				}						}			myXmlHttpRequest.send(data);		}		}</script></head><center><h1>聊天室(您正在和<font color="red"><?php echo $username; ?></font>聊天)</h1>	<textarea cols="30" rows="10" id="mycons"></textarea><br>	<input type="text" style="width:200px" id="con">	<input type="button" value="send" onclick="sendMessage()"></center></html>
Copy after login




<?php	include 'SqlHelper.class.php';	class MessageService{		//将数据信息添加到数据库		function addMessage($sender,$getter,$con){			$sql="insert into message (sender,getter,content,sendTime) values('$sender','$getter','$con',now())";					//	file_put_contents("d:/test.txt","sql=".$sql."\r\n",FILE_APPEND);					$sqlHelper=new SqlHelper();			return $sqlHelper->execute_dml($sql);										}		//获取信息,并把数据组装好返回给客户端(聊天室)		function getMessage($getter,$sender){			$sql="select * from message where getter='$getter' and sender='$sender' and isGet=0";					$sqlHelper=new SqlHelper();			$array=$sqlHelper->execute_dql2($sql);			$messageInfo="<meses>";			for($i=0;$i< count($array);$i++){				$row=$array[$i];				$messageInfo.="<mesid>{$row['id']}</mesid><sender>{$row['sender']}</sender><getter>{$row['getter']}</getter><con>{$row['content']}</con><sendTime>{$row['sendTime']}</sendTime>";			}			$messageInfo.="</meses>";				file_put_contents("d:/test.txt",$messageInfo."\r\n",FILE_APPEND);			$sqlHelper->close_connect();			return $messageInfo;		}	}	
Copy after login





<?php//这个控制器专门响应用户取数据的请求	header("conntent-type:text/xml;charset=utf-8");	header("Cache-Control:no-cache");	$getter=$_POST['getter'];	$sender=$_POST['sender'];	//	file_put_contents("d:/test.txt",$sender."-".$getter,FILE_APPEND);	require_once 'MessageService.class.php';	$messageService=new MessageService();	$mesList=$messageService->getMessage($getter,$sender);//	file_put_contents("d:/test.txt","news--".$mesList."\r\n",FILE_APPEND);	echo $mesList;?>
Copy after login




为什么在第一段函数getMessage()中
var cons=mesRes.getElementsByTagName("con");
alert时为0.数据库中有添加了记录
我看的是韩顺平的php视频。
求大神搭救


回复讨论(解决方案)

在IE中getElementsByName()是根据ID来取的,但在其他浏览器中,是正常按name属性来取的,因此如果你的代码运行在非IE浏览器上,就会出现取不到值的现象

抱歉,没留意你写的是 getElementsByTagName() 你应该改成 getElementById()
var cons=mesRes.getElementById("con");
var sendTimes=mesRes.getElementById("sendTime");
另外你还有不少低级的错误
$('mycons').value+="说:"+cons[i].childNodes[0].nodeValue+" "+sendTimes[i].childNodes[0].nodeValue;
php代码是不会被客户端浏览器执行的,而且你得到的 cons, sendTimes 都只是一个DOM对象,而不是一个结果集

抱歉,没留意你写的是 getElementsByTagName() 你应该改成 getElementById()
var cons=mesRes.getElementById("con");
var sendTimes=mesRes.getElementById("sendTime");
另外你还有不少低级的错误
$('mycons').value+="

很感谢你的指导。新手呢,我今天知道问题出在哪了

 header("conntent-type:text/xml;charset=utf-8");    header("Cache-Control:no-cache");
Copy after login

这里是content-type打多了个n

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template