1

Straight to the case.

This is some functions from my model (Student Model):

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

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

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

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

This is a function from the Student Controller:

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

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

This is how I access the $examdata value via Ajax:

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;
        }
    });
}

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.

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

NashguL
  • 49
  • 1
  • 8
  • have you heard about hidden inputs or inputs in general? – madalinivascu Feb 16 '16 at 08:13
  • if you set `dataType: 'json',` in your javascript, jquery will deserialize your response automatically. (see here: http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc) – Florian Moser Feb 16 '16 at 08:13
  • @FlorianMoser This is a nice hint. In order to access the data, I have to specified the dataType first, so then Ajax can translate that data structure. (CMIIW) – NashguL Feb 16 '16 at 11:42
  • @madalinivascu Are those the one that usually used in the View? I used a couple of hidden indexes in the input data form? – NashguL Feb 16 '16 at 11:45

1 Answers1

1

Add another index for your $examrecord_id

if (isset($session->studentdetails) && !empty($session->studentdetails))
{
    $loggeduser = (object)$session->studentdetails;
    $examrecord_id = $this->student_model->start_exam($exam_id, $loggeduser->student_id);
}
echo json_encode(array(
    'examdata' => $examdata,
    'examrecord_id' => (!empty($examrecord_id)?$examrecord_id:0)
));

Note the shorthand if condition to check if $examrecord_id is empty

Add a dataType option with 'json' as it's value. Then you can access the data

dataType : 'json',
success: function(response)
{
    var data = response.examdata;
    alert(response.examrecord_id); // your examrecord_id
    examId = data.id;
    examName = data.name;
    examDuration = data.duration;
}
roullie
  • 2,830
  • 16
  • 26
  • 1
    I just tried this and it worked. I appreciate this. Thank you very much! This is actually my first project in order to study programming. I still have a lot to learn. The way you showed it with alert so that I can notice it immediatelly is just awesome! – NashguL Feb 16 '16 at 11:48