lynn.wang 发表于 2012-2-20 16:44:53

关于JQuery ajax 的返回值问题

目前正在做一个顶踩   功能
在点击顶的时候,通过 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;
                $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;
                        $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);
}
}

求高手指点!
页: [1]
查看完整版本: 关于JQuery ajax 的返回值问题