【问题标题】:Codeigniter if data inserted return true to json again ? it's possible?Codeigniter 如果插入的数据再次返回 true 到 json 吗?这是可能的?
【发布时间】:2015-12-25 11:50:57
【问题描述】:

我正在使用 json 将数据发布到控制器,但如果数据成功插入数据库,我无法做一些效果,

这个 if 语句在这个 javascript 代码中不起作用? 我的意思是 .like-btn .html() 不起作用,但数据插入到数据库中?

我在 0 到 10 岁之间的 Javascript 经验:D

使用 Codeigniter 3.0.3

这是我的 javascript

<script type="text/javascript">
$(document).ready(function() {
    $(".like-btn").click(function(event) {
    var liker_id = "<?php echo $this->session->userdata('id'); ?>";
    var post_id = $(this).attr('post-id');
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url('home/AddLike'); ?>",
            dataType: 'json',
            data: {liker_id: liker_id, post_id: post_id},
            success: function(res) {
            if (res)
               {
                  $('.like-btn[post-id = '+post_id+']').html('<span class="fa fa-check"></span> Liked');
               }
            }
        });
    });
});
</script>

这是我的控制器

function AddLike() {
    $this->load->helper('string');
    $this->load->model('users_model');
    $this->users_model->Add_like();
}

这是我的模型方法

function Add_like() {
        
        $this->db->where('liker_id', $this->input->post('liker_id'));
        $this->db->where('post_id', $this->input->post('post_id'));
        $query = $this->db->get('likes_table');     
        if($query->num_rows() == 0) {
            $data = array(
                'liker_id'              => $this->input->post('liker_id'),
                'post_id'               => $this->input->post('post_id')
            );
            $this->db->insert('likes_table', $data);
            return true;
        }
    }

【问题讨论】:

  • javascript 中res 的输出是什么?使用 console.log() 查看结果。
  • 你没有从你的function AddLike() 返回任何值,你需要像$result = $this-&gt;users_model-&gt;Add_like(); echo $result ?: false; exit; 一样传递它
  • @DS9 res 未定义

标签: javascript php json codeigniter


【解决方案1】:

只需在您的模型上更改以下内容:

return 更改为echo

function Add_like() {

    $this->db->where('liker_id', $this->input->post('liker_id'));
    $this->db->where('post_id', $this->input->post('post_id'));
    $query = $this->db->get('likes_table');     
    if($query->num_rows() == 0) {
        $data = array(
            'liker_id'              => $this->input->post('liker_id'),
            'post_id'               => $this->input->post('post_id')
        );
        $this->db->insert('likes_table', $data);
        echo true;
    }
}

【讨论】:

  • 是的,你得到它但不是在模型中我在模型中更改它不起作用但是当我更改时返回 true;呼应真实;在控制器工作谢谢你
【解决方案2】:

您应该调试您的 ajax 响应代码。你应该检查你是否在 res 变量中得到任何响应。

重要
1) 在 if 条件下,您应该使用双引号 ("") 将变量 post_id 包裹起来,例如
$('.like-btn[post-id = "'+ post_id+'"]').html...
2) 另一件事是您应该将 "true" 作为字符串而不是布尔值返回,并在 ajax 成功块中检查字符串。

【讨论】:

    【解决方案3】:

    已解决在控制器方法末尾添加这一行

    echo true;
    

    编辑后的代码

    function AddLike() {
    
        $this->load->helper('string');
        $this->load->model('users_model');
        $this->users_model->Add_like();
        echo true;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多