【问题标题】:Script for comments and rating AJAX用于评论和评分 AJAX 的脚本
【发布时间】:2012-02-14 13:17:40
【问题描述】:

是否有任何脚本可以编辑并插入到我的 CI... 我希望能够添加评论和评级并将其保存到数据库而无需重新加载页面..

【问题讨论】:

    标签: php codeigniter jquery


    【解决方案1】:

    ajax 调用 -> ci 控制器 -> 控制器验证 -> 控制器调用模型 -> 模型句柄 db -> ci 将 true/false 返回给 ajax -> 相应的 ajax 句柄。

    AJAX 调用:

    $.ajax({
        url: '/index.php/ratings/process',
        type: 'POST',
        dataType: 'json',
        error: function(data){
          console.log('your errors are: '+data.errors);
        },
        success: function(data){
          console.log('added rating/comments to db!');
        }
    });
    

    CodeIgniter 控制器称为评级:这没有任何表单验证,你应该这样做。在调用它之前,或者使用条件语句来验证。

    public function process(){
        $field1 = $this->input->post('field1', TRUE);
        $params = array( 'field1' => $field1);
        if (!$this->comments->addComments($params)){
          return $this->output->set_status_header(500, 'error submitting to db'); 
        }else{
          return $this->output->set_status_header(200, 'success!'); 
        }
    }
    

    称为 cmets 的 CodeIgniter 模型

    public function addComments($params = FALSE){
      // if params is not equal to false, do work.
      if (!$params == FALSE){
        //insert the params into the database comments
        $this->db->insert('comments', $params);
        // if affected rows is greater than 0, return the last inserted ID.
        if ($this->db->affected_rows() > 0){
           return $this->db->insert_id();
        }else{
           // otherwise return false, fail
           return false;
      }else{
        // params were false so return false
        return false;
      }
    }
    

    不完整的代码,但你明白了。

    【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2012-11-14
    • 2019-04-25
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    相关资源
    最近更新 更多