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.

4
  • have you heard about hidden inputs or inputs in general? Commented Feb 16, 2016 at 8:13
  • if you set dataType: 'json', in your javascript, jquery will deserialize your response automatically. (see here: stackoverflow.com/questions/8517071/…) Commented Feb 16, 2016 at 8: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) Commented Feb 16, 2016 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? Commented Feb 16, 2016 at 11:45

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.