doudou0111
2018-03-07 03:44
浏览 25

如何将数据json从ajax回显到modal js codeigniter?

i want to display data from ajax into model show with echo php, i have made ajax and have working no errors.

this is my model

public function detail($id_branch) {
    $this->db->select('TR_BRANCH.*');
    $this->db->from('TR_BRANCH');
    $this->db->where('BRANCH_ID', $id_branch);
    $query = $this->db->get();
    return $query->result();

}

my controller

Public function detail($id_branch){
    $data = $this->main_model->detail($id_branch);
    echo json_encode($data);
    $this->load->view('template/pages/home_index', $data);
}

my ajax

function coba(id_branch) {
    $('#placeholder').hide();

    $('#placeholder').show(function(){

        $.ajax({
            url:"<?php echo base_url() ?>home/detail/" + id_branch,
            success(res){
                // alert(id_branch)
            }
        })


    });

}

my view

<div id="placeholder">
<div class="row">
    <div class="panel panel-default">
        <div class="panel-heading clearfix">
         <p><?php echo $data->DISPLAY_NAME; ?></p>
        </div>
    </div>
</div>

if i

<?php echo $data->DISPLAY_NAME; ?> 

in my modal, i'm getting error like Undefined variable: data. result data json_encode already appear in network based id_branch. how to fix it? pls help

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubi6898 2018-03-07 05:25
    已采纳

    As you are getting DISPLAY_NAME in Ajax request, so you cannot use $data->DISPLAY_NAME,

    You need to give id to p tag & update its value in ajax success method

    <p id="displayName"></p>
    
    
    success(res) 
    {
        // alert(id_branch)
         $("#displayName").html(Read Value from res data);
    }
    
    已采纳该答案
    打赏 评论
  • doutenglou6588 2018-03-07 04:49

    See: https://codeigniter.com/user_guide/general/views.html

    View receives $data keys (and their value) as pieces, not whole the $data variable.

    So, in your "my view", it should be echo $DISPLAY_NAME;.

    Hope this help!

    打赏 评论
  • dqj96395 2018-03-07 05:28

    I assume you want to return the view to ajax, and that you only want to get one record. Do the following:

    public function detail($id_branch) {
        $this->db->select('TR_BRANCH.*');
        $this->db->from('TR_BRANCH');
        $this->db->where('BRANCH_ID', $id_branch);
        $query = $this->db->get();
        return $query->row();
    
    }
    

    Controller:

    public function detail($id_branch){
        $data = $this->main_model->detail($id_branch);
        //echo json_encode($data); // why??
        $this->load->view('template/pages/home_index', $data);
    }
    
    打赏 评论

相关推荐 更多相似问题