pinglun.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function initXHR()
{
return window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
}
function setURL(serverUrl,getNo,elementID)
{
var text=document.getElementById(elementID).value;
var url=serverUrl+"?"+getNo+"="+encodeURI(text)+"&r="+Math.random();
return url;
}
function testXHR()
{
var xhr=initXHR();
var url=setURL("server.php","content","c");
xhr.open("get",url,true);
xhr.send(null);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
document.getElementById("div").innerHTML+=xhr.responseText+"<br>";
return true;
}
}
}
</script>
</head>
<body>
<img src="a.jpg" width="500" height="300" /><hr />发表评论:<br />
<div id="div" style="background:#CCC">
<?php
include("connect.php");
$result=mysql_query("select * from c");
while($row=mysql_fetch_row($result))
{
echo "评论".$row[0].":".$row[1]."<br>";
}
?>
</div>
<form action="server.php" method="post" >
<textarea id="c" name="c" rows="5" cols="60"></textarea><br />
<input type="button" value="发表" onclick="testXHR()"/>
</form>
</body>
</html>
server.php
<?php
include("connect.php");
$q="insert into c(comments) values('".$_GET["content"]."')";
mysql_query($q);
$re=mysql_query("select id from c order by id desc limit 1");
$row=mysql_fetch_row($re);
echo "评论".$row[0].":".$_GET["content"];
?>
connect.php
<?php
$link = mysql_connect('localhost','root','123') or die("数据库连接失败!");//连接数据库
mysql_select_db('pinglun',$link);//选择数据库
mysql_query("set names utf8");//设置编码格式
?>