简体   繁体   English

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

[英]how to send multiple radio button values via ajax to 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. 我想将其值传递给php脚本,如何传递每个单选按钮组的值,如果mysql查询返回10行,则有10个单选按钮组,我需要发送所有单选按钮的值。 And my ajax call is 我的ajax电话是

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

You can use $(form_selector).serialize() that serializes all the values of the form and send to your server as an intended input. 您可以使用$(form_selector).serialize()来序列化表单的所有值,并将其作为预期的输入发送到服务器。

$("#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: 您可以使用PHP通过以下方式获得此功能:

$_POST["student-1"]  // absent
$_POST["student-2"]  // present
$_POST["student-3"]  // absent
$_POST["student-4"]  // present
$_POST["student-5"]  // absent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM