How to pass variable from Controller to View with Ajax in Codeigniter?
<p>I have a problem with passing data from Controller to View.</p>

<p>I am using Ajax to do this, you can see my Ajax code here:</p>

<pre><code>$(document).ready(function(){
    $('li.thang').click(function(){
        var id_thang = $(this).attr('value');
        $.ajax({
            url: baseUrl+'/Home/getImage',
            dataType: 'json',
            type: 'POST',
            data: {id_thang: id_thang},
        }).done(function(result) {
            console.log(result.get_list_image_thang);
        })          
    });
});
</code></pre>

<p>I will get <code>id_thang</code> when clicking in HTML tags <code>li &gt; thang</code>.</p>

<p>At <code>Controller / Home.php</code>, I will get an array data on this <code>id_thang</code>.</p>

<pre><code>function getImage(){
    $id_thang = $this-&gt;input-&gt;post('id_thang');
    $input = array();
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this-&gt;Mmenushoatnao-&gt;get_info($id_thang);
    ob_start();
}
</code></pre>

<p>All data is stored in array <code>$get_image_thang</code>.</p>

<p>Finally, I don't know how to pass this array to View show all data I selected.</p>

<p>In View/index.php I am trying a <code>foreach</code> loop through all data in this array and show in <code>&lt;html&gt;</code> tag. Like this:</p>

<pre><code>&lt;?php foreach($get_image_thang AS $item) ?&gt;
    &lt;div class="col-md-4 col-sm-4 col-xs-4"&gt;
        &lt;?php echo $item-&gt;id; ?&gt;
    &lt;/div&gt;
&lt;?php endforeach ?&gt;
</code></pre>

<p>Note: at <code>View / index.php</code> is demo code.</p>

<p>Problem is I don't know how to send <code>$get_image_thang</code> to this View.</p>

<p>Update 1: </p>

<p>I tried to put: <code>console.log(result);</code> to <code>.done(function(result)</code> event and receive result like this:</p>

<p><a href="https://i.stack.imgur.com/ruYuM.png" rel="nofollow"><img src="https://i.stack.imgur.com/ruYuM.png" alt=""></a></p>

<p>Problem is: I use <code>row +=  result[i].id;</code> or any property like <code>id</code>, <code>name</code>, <code>image_list</code> is not undefined.</p>

<p><strong>Update 2:</strong></p>

<p>Only two function to get info base on <code>id</code>. I write all code in <code>core/MY_MODEL.php</code>:</p>

<pre><code>function get_info($id, $field = '')
{
    if (!$id)
    {
        return FALSE;
    }

    $where = array();
    $where[$this-&gt;key] = $id;

    return $this-&gt;get_info_rule($where, $field);
}

function get_info_rule($where = array(), $field= '')
{
    if($field)
    {
        $this-&gt;db-&gt;select($field);
    }
    $this-&gt;db-&gt;where($where);
    $query = $this-&gt;db-&gt;get($this-&gt;table);
    if ($query-&gt;num_rows())
    {
        return $query-&gt;row();
    }

    return FALSE;
}
</code></pre>

<p>At controller, I call <code>get_info</code>. Note:</p>

<p><code>Mmenushoatnao</code> is a Model maps in database.</p>

<p><strong>Update 3:</strong></p>

<p>I only know write code in <code>Controller</code> to get data after <code>click</code> event.</p>

<p>But like your question mention. Must write all code in Ajax code.</p>

<p>Like this:</p>

<pre><code>function getImage(){
    $id_thang = $this-&gt;input-&gt;post('id_thang');
    $input = array();
    $input['order'] = array('id','ASC');
    $get_image_thang  = $this-&gt;Mmenushoatnao-&gt;get_info($id_thang);
    ob_start();
    ?&gt;
    &lt;?php foreach($get_image_thang as $item): ?&gt;
    &lt;?php $image_list = json_decode($item-&gt;image_list); ?&gt;
    &lt;?php foreach($image_list as $image): ?&gt;
    &lt;div class="col-md-4 col-sm-4 col-xs-4"&gt;
        &lt;img src="&lt;?php echo upload_url() ?&gt;/img/hoatnao/hinhanh/&lt;?php echo $image ?&gt;" alt="Image" class="img-responsive"&gt;
    &lt;/div&gt;
    &lt;?php endforeach; ?&gt;
    &lt;?php endforeach; ?&gt;

    &lt;?php
    $return  = ob_get_clean();
    $data['result']=$return;
    echo json_encode($data);
}
</code></pre>

<p>Of course, this code is not working.</p>

<p>So, we need convert to Ajax code.</p>