codeigniter:获取ajax结果问题

时间:2015-04-19 12:39:14

标签: php jquery ajax codeigniter

我有这个简单的考试系统,我想在用户回答时使用ajax

这是我的表单

  

当我使用action ="< *?= site_url("考试/ check_answer")?>"它给出了302错误

<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>

这是我的控制器

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



    }

这是我的模特

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

}

3 个答案:

答案 0 :(得分:1)

首先检查你的路线是否有任何网址重定向? 并且您的表单必须提交给控制器功能

答案 1 :(得分:0)

但为什么要使用表单提交与ajax一起使用? 试试这个

<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>

并使用此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>

答案 2 :(得分:0)

路由配置:

$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>

和表格:

<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>