投票ajax请求代码(点赞代码)
1 function vote(url, arr) { 2 jq.ajax({ 3 cache: false, 4 async: false, 5 url: url, 6 type: 'post', 7 data: {info_arr: arr}, 8 dataType: 'json', 9 error: function (a) { 10 }, 11 success: function (json) { 12 if (json.status == 1) { 13 alert(json.info); 14 //直接刷新页面,不用ajax 15 location.reload(); 16 } else { 17 alert(json.info); 18 return false; 19 } 20 } 21 }); 22 }
后台处理代码:
1 public function pzan(){ 2 $ip = get_client_ip(); //获取用户IP 3 $id = $_POST['id']; 4 if(!isset($id) || empty($id)) exit; 5 $map['pid'] = $id; 6 $map['ip'] = $ip; 7 $zan = M('zan_ip'); 8 $count = $zan->where($map)->count(); 9 if($count){ 10 //已经存在记录,赞过了。 11 $info['status'] = 0; 12 $info['msg'] = '已经赞过咯!'; 13 $this->ajaxReturn($info); 14 }else{ 15 //第一次赞 16 $map1['id'] = $id; 17 $p = M('project');//查看是否已经赞过这个文章 18 $p->where($map1)->setInc('points',1); 19 $data['pid'] = $id; 20 $data['ip'] = $ip; 21 $zan->create($data); 22 $zaninfo = $zan->add(); 23 if ($zaninfo) { 24 $info['status'] = 1; 25 $info['msg'] = '点赞成功!!'; 26 $this->ajaxReturn($info); 27 } 28 29 } 30 31 }
你要保守你心,胜过保守一切。
本文来自博客园,作者:刘俊涛的博客,转载请注明原文链接:https://www.cnblogs.com/lovebing/p/6823455.html