从多个复选框中获取 $_POST [英] Get $_POST from multiple checkboxes

查看:33
本文介绍了从多个复选框中获取 $_POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 1 个表单,其中有多个复选框(每个都有代码):

I have 1 form in with multiple checkboxes in it (each with the code):

<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">

其中 $row['Report ID'] 是数据库中的主键 - 因此每个值都不同.

Where $row['Report ID'] is a primary key in a database -so each value is different.

我如何才能知道哪些复选框已被选中?(可能有多个)

How would I be able to tell which checkboxes have been checked? (Maybe multiple)

这是一个收件箱系统,我下面有一个按钮,我想(点击时)删除所有带有复选框的消息(ID:$row['Report ID'])已检查.

This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.

推荐答案

将表单中的名称设置为check_list[],您将能够以数组的形式访问所有复选框($_POST['check_list'][]).

Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

这是应要求的一个小样本:

Here's a little sample as requested:

<form action="test.php" method="post">
    <input type="checkbox" name="check_list[]" value="value 1">
    <input type="checkbox" name="check_list[]" value="value 2">
    <input type="checkbox" name="check_list[]" value="value 3">
    <input type="checkbox" name="check_list[]" value="value 4">
    <input type="checkbox" name="check_list[]" value="value 5">
    <input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
            echo $check; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}
?>

这篇关于从多个复选框中获取 $_POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆