dongsu1539 2015-12-08 15:35
浏览 66
已采纳

如何通过ajax向php发送多个单选按钮值

I have multiple radio buttons differentiated by their name, here is my script

<?php
            if(mysqli_num_rows($result)>0){
                while($row = $result->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $row['fullname'];?></td>
                        <td><?php echo $row['email'];?></td>
                        <td><?php echo $row['class'];?></td>
                        <td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
                        <td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
                    </tr>
                <?php }
            }
            ?>

I want to pass their value to a php script, how I can pass each radio group value, Say if mysql query returned 10 rows then there are 10 radio button groups and I need to send all values of radio buttons. And my ajax call is

$("#submitAttendance").click(function(){
                $.ajax({
                    url: 'teacher.php',
                    method: 'post',
                    data:
                });
            }); 
  • 写回答

1条回答 默认 最新

  • duangu1033 2015-12-08 15:41
    关注

    You can use $(form_selector).serialize() that serializes all the values of the form and send to your server as an intended input.

    $("#submitAttendance").click(function(){
        $.ajax({
            url: 'teacher.php',
            method: 'post',
            data: $(form_selector).serialize()
        });
    });
    

    Also, make sure you write this code in:

    $(form_selector).submit()
    

    Rather than attaching an event handler to the submit button.

    So, a typical output of your form, say like this:

    <form action="">
      <div>
        <label><input type="radio" name="student-1" id="" value="present"> Present</label>
        <label><input type="radio" name="student-1" id="" value="absent"> Absent</label>
      </div>
      <div>
        <label><input type="radio" name="student-2" id="" value="present"> Present</label>
        <label><input type="radio" name="student-2" id="" value="absent"> Absent</label>
      </div>
      <div>
        <label><input type="radio" name="student-3" id="" value="present"> Present</label>
        <label><input type="radio" name="student-3" id="" value="absent"> Absent</label>
      </div>
      <div>
        <label><input type="radio" name="student-4" id="" value="present"> Present</label>
        <label><input type="radio" name="student-4" id="" value="absent"> Absent</label>
      </div>
      <div>
        <label><input type="radio" name="student-5" id="" value="present"> Present</label>
        <label><input type="radio" name="student-5" id="" value="absent"> Absent</label>
      </div>
    </form>
    

    Would be:

    "student-1=absent&student-2=present&student-3=absent&student-4=present&student-5=absent"
    

    Is this what you are looking for? And you can get this using PHP using:

    $_POST["student-1"]  // absent
    $_POST["student-2"]  // present
    $_POST["student-3"]  // absent
    $_POST["student-4"]  // present
    $_POST["student-5"]  // absent
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况