• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php结合ajax实现赞、顶、踩功能实例_php实例

php 搞代码 4年前 (2022-01-26) 34次浏览 已收录 0个评论

php应用越来越多,也越来越广泛,为了增加网站的丰富多彩,从而出现了很多新的技术。ajax是现代网站中不能缺少的一项技术,他可以异步刷新数据,而实现很多效果,比如刷新验证码,微博中的赞功能,都是运用这个。

本次赞功能的效果图:

主页文件(index.php):

<script type="text/javascript" src="http://www.gaodaima.com/js/jquery.min.js"></script><BR><script type="text/javascript" src="finger_ajax.js"></script><BR><?php<BR> header("Content-type:text/html;charset=utf-8");<BR> include "finger_ajax.php";</P><P> $sql = "select * from finger_ajax";<BR> $res = mysql_query($sql,$link);<BR> while($row = mysql_fetch_array($res)){<BR>  echo "<p>".$row['title']." \r\n赞一下(<span>".$row['finger']."</span>)</p>\r\n";<BR> }<BR>?>

处理ajax请求及配置信息文件(finger_ajax.php):

<?php<BR> /**"赞" 功能 响应ajax请求*/<BR> //配置<BR> $dbHost = "localhost";<BR> $dbUser = "root";<BR> $dbPass = "dddddd";<BR> $dbName = "test";<BR> $dbCharset = "utf8";</P><P> $link = mysql_connect($dbHost,$dbUser,$dbPass) or die(mysql_error());<BR> my<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i><strong>搞代gaodaima码</strong>sql_query("set names ".$dbCharset);<BR> mysql_select_db($dbName);<BR> // End</P><P> //接受对应的id<BR> if(!empty($_POST['id'])){<BR>  $id = $_POST['id'];<BR>  //“赞”加1<BR>  $sql = "update finger_ajax set finger=finger+1 where id=$id;";<BR>  if(mysql_query($sql,$link)){<BR>   echo "ok";<BR>  }else{<BR>   echo "failed";<BR>  }<BR> }<BR>?>


js文件(finger_ajax.js):

//赞 js<BR>function finger(topic_id){<BR> $.post("finger_ajax.php", { "id": topic_id },<BR>   function(data){<BR>     if(data=="ok"){<BR>    alert("感谢您的支持!");<BR>   }else{<BR>    alert("对不起,失败了!");<BR>   }<BR>   }, "text");  <BR>  //获取当前“赞”的次数并加1<BR>  var finger = parseInt($(".finger"+topic_id).html())+1;<BR>  //更新“赞”的次数<BR>  $(".finger"+topic_id).html(finger);<BR>}<BR>


数据库代码(finger_ajax.sql):

DROP TABLE IF EXISTS `finger_ajax`;<BR>CREATE TABLE `finger_ajax` (<BR>  `id` int(11) NOT NULL AUTO_INCREMENT,<BR>  `title` varchar(50) NOT NULL DEFAULT '',<BR>  `finger` int(11) NOT NULL DEFAULT '0',<BR>  PRIMARY KEY (`id`)<BR>) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;</P><P>-- ----------------------------<BR>-- Records of finger_ajax<BR>-- ----------------------------<BR>INSERT INTO `finger_ajax` VALUES ('1', '今天天气还不错哦!去做点什么好呢?', '10');<BR>INSERT INTO `finger_ajax` VALUES ('2', '欢迎来到 www.gaodaima.com,国庆将至,祝大家国庆节快乐!!', '3');

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php结合ajax实现赞、顶、踩功能实例_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址