Passing a value from Controller to jQuery CodeIgniter
<p>Straight to the case.</p>

<p>This is some functions from my model (Student Model):</p>

<pre><code>public function get_exam_data($exam_id){
    $this-&gt;db-&gt;select('exam_id, exam_name, duration');
    $this-&gt;db-&gt;from('exams');
    $this-&gt;db-&gt;where('exam_id', $exam_id);
    $result = $this-&gt;db-&gt;get();

    $exam = array();
    $examrow = $result-&gt;row();
    $exam['id'] = $examrow-&gt;exam_id;
    $exam['name'] = $examrow-&gt;exam_name;
    $exam['duration'] = $examrow-&gt;duration;
    return $result;
}

public function start_exam($exam_id, $student_id)
{
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('exam_record');
    $exam_newstatus = array(
        'student_id' =&gt; $student_id,
        'exam_id' =&gt; $exam_id);
    $this-&gt;db-&gt;set('start_time', 'NOW()', FALSE);
    $this-&gt;db-&gt;insert('exam_record', $exam_newstatus);

    //$examrecord_id is autoincrement in mysql exam_record table
    $examrecord_id = $this-&gt;db-&gt;insert_id();
    return $examrecord_id;
}
</code></pre>

<p>This is a function from the Student Controller:</p>

<pre><code>public function get_student_exam_data()
{
    $exam_id  = $this-&gt;input-&gt;post('examId');
    $examdata = $this-&gt;student_model-&gt;get_exam_data($exam_id);
    $session = get_session_details();

    if (isset($session-&gt;studentdetails) &amp;&amp; !empty($session-&gt;studentdetails))
    {
        $loggeduser = (object)$session-&gt;studentdetails;
        $examrecord_id = $this-&gt;student_model-&gt;start_exam($exam_id, $loggeduser-&gt;student_id);
    }
    echo json_encode($examdata);
}
</code></pre>

<p>This is how I access the $examdata value via Ajax:</p>

<pre><code>jQuery(function()
{
    $.ajax({
        type   : "POST",
        url    : "../get_exam_data/",
        async  : false,
        data   : {"examId": EXAM_ID },
        success: function(response)
        {
            var data = $.parseJSON(response);
            examId = data.id;
            examName = data.name;
            examDuration = data.duration;
        }
    });
}
</code></pre>

<p>I want to be able to pass $examrecord_id from the Student Controller to use it on my jQuery file, just like the $examdata.
I tried to use json_encode() twice on the Controller. Didn't work.</p>

<p>How do I pass <strong>$examrecord_id</strong> from the <strong>Controller</strong> to the jQuery file?
Can someone enlighten me, please? Thank you.</p>