Mazen Mezlini
Mazen Mezlini

Reputation: 55

codeigniter : getting ajax result issues

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

}

Upvotes: 0

Views: 146

Answers (3)

Mazen Mezlini
Mazen Mezlini

Reputation: 55

routes config :

$route['exams/check_answer'] = "exams/check_answer";

jquery :

<script type="text/javascript">
    $("#frm-answer").submit(function (e){
        e.preventDefault();
        var url = $(this).attr('action');
        var method = $(this).attr('method');
        var data = $(this).serialize();

        $.ajax({
            url:url,
            type:method,
            data:data
        }).done(function(data){
            if(data.result !== 1)
            {
                $("#true-answer").hide('fast'),
                $("#wrong-answer").show('fast'),
                $("#wrong-answer").effect('shake');
            }else{
                $("#wrong-answer").hide('fast'),
                $("#true-answer").show('fast'),
                $("#true-answer").effect("shake");
            }
             });
          });
</script>

and the form :

<form action="<?=site_url('exams/check_answer');?>" method="post" id="frm-answer">
                    <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="exam_id" id="exam_id" value="<?php echo $result->id;?>">

                        <input type="submit"  class="btn btn-default sub" value="اجابة">
                    </div>

                </form>

Upvotes: 0

S Vinesh
S Vinesh

Reputation: 539

But why should you use form submit to go with ajax ? Try this

<form>
  <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" id="exam_id" value="<?php echo $result->id;?>">

   <input type="button"  class="btn btn-default sub" value="اجابة">
  </div>

   </form>

and use this jquery ajax

<script>
    $('.sub').click(function(){
        var selected_val = $('input[name=response]').val();
        var exam_id   = $('#exam_id').val();
         $.post("<?php echo site_url('exams/check_answer'); ?>",
         {
            response:selected_val,
            exam_id :exam_id
         },
         function(response){

                      //you will get the response from controller here
         });


    });
</script>

Upvotes: 0

Islam Zedan
Islam Zedan

Reputation: 656

first Check your routes if making any url redirects? and your form must submit to controller function

Upvotes: 1

Related Questions