【问题标题】:how to send multiple radio button values via ajax to php如何通过ajax将多个单选按钮值发送到php
【发布时间】:2015-12-08 23:46:17
【问题描述】:

我有多个按名称区分的单选按钮,这是我的脚本

<?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 }
            }
            ?>

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

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

【问题讨论】:

  • #submitAttendance 在哪里?请粘贴完整代码。
  • 您要发送所有值还是选定值?前者有点奇怪。如果是后者,请使用$('form').serialize()
  • @RoryMcCrossan 你混淆了前者和后者。
  • 不,这是正确的方法。
  • 我只想发送所有选定的值并且我没有使用表单?还有其他方法吗? @RoryMcCrossan

标签: php jquery ajax radio-button


【解决方案1】:

您可以使用$(form_selector).serialize() 序列化表单的所有值并作为预期输入发送到您的服务器。

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

另外,请确保您在以下位置编写此代码:

$(form_selector).submit()

而不是将事件处理程序附加到提交按钮。

所以,你的表单的典型输出是这样的:

<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>

应该是:

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

这就是你要找的吗?你可以使用 PHP 来获得这个:

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

【讨论】:

  • serialize() 仅适用于表单?或者我也可以$("div").serialize();
  • 至少尝试一次
猜你喜欢
  • 2015-08-23
  • 2013-03-26
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
相关资源
最近更新 更多