通过PHP中的AJAX复选框值和单选按钮值插入数据

时间:2019-03-28 21:56:53

标签: php ajax mysqli

一切正常,但是我想使用单选按钮值将$result['id'];插入数据库。

JavaScript:

<script>
    $(document).ready(function() {
        $('input[type="radio"]').change(function() {
            var stu_ans = $(this).val();
            var stu_id = "<?php echo $_GET['sid'];?>";
            var q_id = "<?php echo $result['id'];?>";
            $.ajax({
                url: "insert1.php",
                method: "POST",
                data: {
                    stu_ans: stu_ans,
                    stu_id: stu_id,
                    q_id: q_id
                },
                success: function(data) {
                    $('#result').html(data);
                }
            });
        });
    }); 
</script> 

HTML:

<form  role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
  <?php
    $number_question = 1;
    $row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
    $rowcount = mysqli_num_rows( $row );
    $remainder = $rowcount/$number_question;
    $i = 0;
    $j = 1; $k = 1;
    ?>
  <h4>Total Question:-<?php echo $rowcount;?></h4>
  <?php 
    while ( $result = mysqli_fetch_assoc($row) ) {
      if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";
  ?>
  <div id='question<?php echo $k;?>' >
    <p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
    <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <style>
            input[type="radio"] {
            -ms-transform: scale(1.5); /* IE 9 */
            -webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
            transform: scale(1.5);
            }
          </style>
          <tr>
            <td>(A)<?php echo trim($result['o1']);?></td>
            <td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
          </tr>
          <tr>
            <td>(B)<?php echo $result['o2'];?>
            </td>
            <td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
          </tr>
          <tr>
            <td>(C)<?php echo $result['o3'];?></td>
            <td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
            </td>
          </tr>
          <tr>
            <td>(D)<?php echo $result['o4'];?></td>
            <td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>                                                                      
    <br/>
  </div>
  <?php
    $i++; 
    if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
       echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
       echo "</div>";
    }  elseif ( $rowcount > $number_question  ) {
       if ( $j == 1 && $i == $number_question ) {
           echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
           echo "</div>";
           $i = 0;
           $j++;           
       } elseif ( $k == $rowcount ) { 
           echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
                       <button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
           echo "</div>";
           $i = 0;
           $j++;
       } elseif ( $j > 1 && $i == $number_question ) {
           echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
                       <button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
           echo "</div>";
           $i = 0;
           $j++;
       }
    }
     $k++;
    } 
  ?>
</form>

1 个答案:

答案 0 :(得分:1)

在代码中保留SQL注入和安全性内容...

更改

var stu_ans=$(this).val(); 

针对:

var stu_and =$("input[name='<name_of_radio>']:checked").val();

这是从选定的单选按钮获取值的方法。

然后请!!!看看安全问题。

希望此代码对您有所帮助!