我无法使用ajax将无线电的值发送到另一个页面

时间:2016-09-07 10:54:14

标签: php ajax

我想使用ajax在另一个页面上发送单选按钮的值,但我无法做到。

   <?php
      session_start();
      $i=1;
      if(isset($_SESSION['email']))
   {
    include('config.php');
    $response=mysql_query("select * from questions where subject='".$_SESSION['subject']."'");
      ?>
     <div id="page-wrapper">
     <div class="col-md-12 graphs">
     <div class="xs">
     <form method="post" id="quiz_form"> 
     <?php while($result=mysql_fetch_array($response)){ ?>
     <?php echo $i.".".$result['question'];?>
     <div>
                    A:- <input type="radio" value="1" id="optiona_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
                    <label for="optiona_<?php echo $result['id'];?>" ><?php echo $result['optiona'];?></label>
                    <br/>
                    B:- <input type="radio" value="2" id="optionb_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
                    <label for="optionb_<?php echo $result['id'];?>" ><?php echo $result['optionb'];?></label>
                    <br/>
                    C:- <input type="radio" value="3" id="optionc_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
                    <label for="optionc_<?php echo $result['id'];?>" ><?php echo $result['optionc'];?></label>
                    <br/>
                    D:- <input type="radio" value="4" id="optiond_<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" >
                    <label for="optiond_<?php echo $result['id'];?>" ><?php echo $result['optiond'];?></label>
                    <input type="radio" checked='checked' value="5" style='display:none' id='option_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
  </div>
  <?php 
  $i++;
  }?>
  <br />
  <div class="text-center">
  <input type="button" id="btn4"  class="btn-success btn" value="Submit Quiz" />
  </div>
  </form>
  <div id='result'>

  </div>
  </div>
  </div>
  </div>
    <?php
      include('adminfooter.php');
    }
    else {
    $_SESSION['invalid']=='set';
    header('location:login.php');
    }
    ?>
    <script>
        $(document).ready(function(){
        $("#btn4").click(function(){

        $.ajax({
                type: "POST",
                url: "ajax.php",
                data: $("form").serialize(),
                success: function(msg) {
                  $("#quiz_form").addClass("hide");
                  $("#result").show();
                  $("#result").append(msg);
                }
  });
 });
});
   </script>

我认为问题是$("form").serialize()。 这是我的ajax.php

   <?php 
    session_start();

   include('config.php');
     $query = "select * from questions where subject='".$_SESSION['subject']."'";
   $result=mysql_query($query) or die(mysql_error());
    $right_answer=0;
  $wrong_answer=0;
  $unanswered=0;
    $n=1;
  while($response=mysql_fetch_array($result))
   {

  echo $response['answer'];//this showed the answer
   echo $_POST["$n"];//this showed the radio button no of the answer clicked
   if($response['answer']==$_POST["$n"])
  {
  $right_answer++;    
  }
   else if($_POST["$n"]==5)
   {   
     $unanswered++;
    }
  else
  {
    $wrong_answer++;
  }
 $n++;
}
   echo "<div id='answer'>";
   echo " Right Answer  : ". $right_answer."</span><br>";

   echo " Wrong Answer  : ". $wrong_answer."</span><br>";

  echo " Unanswered Question  : ". $unanswered."</span><br>";
  echo "</div>";  
    ?>

错误是,它没有在$n中获得值。它显示未定义的偏移量。

0 个答案:

没有答案