用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2692|回复: 4
收起左侧

[已解决] 【SOS】关于Jquery ajax返回值问题

[复制链接]
发表于 2012-2-20 16:49:24 | 显示全部楼层 |阅读模式
目前正在做一个  顶踩   功能
在点击顶的时候,通过 jquery.ajax 去调用的一个函数去更新数据库
但是我需要根据是否已更新 来给不同的返回值
如果返回更新  界面 + 1
如果返回未更新 界面不变
大体的代码如下:
Controller:
public function prototype()
        {
                // only one need to vote
                $id = 2;
                $this->load->model('M_Vote');
                $row = $this->M_Vote->select_num_byid($id);        

                $this->load->library('Get_Info');
                $result = $this->get_info->get_remote_info();

                $data['id']  = $id;
                $data['num'] = $row->num;
                $data['ip']  = $result[0];
                $this->load->view('prototype_view',$data);
        }

views:
< script type="text/javascript" src="/assets/js/jquery.js"></script>
< script type="text/javascript" src="/assets/js/top.js"></script>
< div>
        <a id="press" href="javascript:void(0)">Top</a>
        <span id="votenum" value="<?php echo $id; ?>"><?php echo $num; ?></span>
< /div>

vote.php:
< ?php
        class Vote extends CI_Controller
        {
                public function index()
                {
                        $id = $_POST['id'];
                        
                        $this->load->library('Get_Info');
                        $result = $this->get_info->get_remote_info();
                        $ip = $result[0];
                        $this->load->model('m_vote');
                        if ($this->m_vote->chk_voted($ip,$id))
                        {
                                $this->m_vote->insert_ip_record($ip,$id);
                                $this->m_vote->upd_num_byid($id);        
                                $row = $this->m_vote->select_num_byid($id);
                                return $row->num;
                        }
                        else
                        {
                                return -1;
                        }
                }
        }
?>

M_Vote.php
class M_Vote extends CI_Model {

        function __construct()
   {
       parent::__construct();
       $this->load->database();
   }
  
  function select_num()
  {
          $this->db->select('*');
                $this->db->from('vote');
                return $this->db->get();
  }
  
  function select_num_byid($id)
  {
          $this->db->select('*');
                $this->db->from('vote');
                $this->db->where('itemid =',"$id");
                $query = $this->db->get();
                return $query->row();
  }
  
  function upd_num_byid($id)
  {
          $ADD_NUM = 1;
    $this->db->set('num',"num+$ADD_NUM",false);
                $this->db->where('itemid', $id);
                 $this->db->update('vote');
  }
  
  function insert_ip_record($ip,$id)
  {
    $data = array (
                                            'ip' => $ip,
                                            'vote_id' => $id,
                                            'votedate' => date("Y-m-d H:i:s")
                                    );
                 $this->db->insert('ip_record',$data);
  }
  
  function chk_voted($ip,$id)
  {
          // check whether voted by ip and id
          $this->db->select('*');
          $this->db->from('ip_record');
          $this->db->where('ip', $ip);
          $this->db->where('vote_id', $id);
          $query = $this->db->get();
          return (($query->num_rows() == 0) ? TRUE : FALSE);
  }
}

求高手指点!
发表于 2012-2-20 17:00:12 | 显示全部楼层
很简单啊
 楼主| 发表于 2012-2-20 17:20:34 | 显示全部楼层
wangjin 发表于 2012-2-20 17:00
很简单啊

怎讲?
发表于 2012-2-20 18:03:40 | 显示全部楼层
你先看看jquery ajax去
发表于 2012-2-21 13:54:27 | 显示全部楼层
JS复制代码
$.ajax({
        type:'post',
        url:'<?=site_url()?>/Permission/addjiben',
        data:str,
        error:function(){
          alert('error');
        },
        success:function(data){
                var html = eval(data);
                if(html.info){
                        jBox.tip('插入成功','success');
                        }
                }else{
                        jBox.tip('插入失败','error');
                        }
        }
});
复制代码

本版积分规则