网站地图    收藏   

主页 > 后端 > PHP语言 >

jquery 无刷新评论

来源:未知    时间:2014-11-06 15:32 作者:xxadmin 阅读:

[导读] 发表评论页面: !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=...

发表评论页面:

 

<!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";}
?>

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论