i have this simple exams system and i want to use ajax when a user answer
this is my form :
when i use action="<*?=site_url("exams/check_answer")?>" it gives 302 error
<form action="<?=site_url('exams/'.$result->id.'/check_answer');?>" method="post" id="answer-exam-frm">
<div>
<p><input type="radio" name="response" value="first"><span class="choice-exam"><?php echo $result->choice1;?></span></p>
<p><input type="radio" name="response" value="second"><span class="choice-exam"><?php echo $result->choice2;?></span></p>
<p><input type="radio" name="response" value="third"><span class="choice-exam"><?php echo $result->choice3;?></span></p>
<input type="hidden" name="examid" value="<?php echo $result->id;?>">
<input type="submit" class="btn btn-default" value="اجابة">
</div>
</form>
this is my controller :
public function check_answer(){
$user_answer = $this->input->post('response') ;
$exam_id = $this->input->post('examid') ;
$result = $this->style_model->check_answer($user_answer,$exam_id) ;
$this->output->set_content_type('application/json') ;
if($result){
$this->output->set_output(json_encode(['result' => 1 ])) ;
return false;
}
$this->output->set_output(json_encode(['result' => 0 ])) ;
}
And this is my model :
public function check_answer($user_answer,$exam_id)
{
$this->db->where('response' , $user_answer);
$this->db->where('id' , $exam_id);
$get = $this->db->get('exam') ;
return $get->result() ;
}
Aucun commentaire:
Enregistrer un commentaire