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

时间:2022-09-25 15:55:25

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

我想将它们的值传递给php脚本,我如何传递每个无线电组值,如果mysql查询返回10行,那么有10个单选按钮组,我需要发送单选按钮的所有值。我的ajax电话是

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

1 个解决方案

#1


0  

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

#1


0  

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