发表评论页面:

<!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=gb2312" />
<title>jQuery - ajax无刷新保存例子</title>
</script>
<script language="JavaScript" type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div class="wrap">
<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="50">用户名</td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>内容</td>
<td><textarea name="comments" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><input name="submit" id="submit" type="submit" value="提交" /></td>
</tr>
</table>
<p></p>
</div>
</body>
</html>

ajxa.js

$(document).ready(function(){        //DOM的onload事件处理函数    
  $("#submit").click(function(){          //当按钮button被点击时的处理函数    
    postdata();                                      //button被点击时执行postdata函数    
  });    
});   
function postdata(){                             //提交数据函数    
  $.ajax({                                                 //调用jquery的ajax方法    
    type: "POST",                                     //设置ajax方法提交数据的形式    
    url: "comments.php",                                      //把数据提交到comments.php    
    data: "username="+$("#username").val()+"&comments="+$("#comments").val(),  
    success: function(msg){                 //提交成功后的回调
if (msg="ok"){
  alert("回复成功!")
  $("p").append($("#username").val()+"<br>"+$("#comments").val())
}else{
  alert("回复失败!")
  }
    }    
  });    
}

comments.php

<?php
require_once("../class.inc/mysql.class.php");
$writer=$_POST['username'];
$comments=$_POST[comments];
if($writer=""||$comments=""){
echo "error";
}else{
$db=new DB_MySQL();
$query=$db->query("insert into comments (username,comments) values($writer,$comments)");
echo "ok";}
?>

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shinian315/archive/2009/04/30/4140337.aspx

posted on 2009-10-13 16:46  sweting  阅读(300)  评论(0编辑  收藏  举报