php 从多个复选框中获取 $_POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4997252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:30:08  来源:igfitidea点击:

Get $_POST from multiple checkboxes

phphtmlformscheckbox

提问by James Rattray

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

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

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

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

$row['Report ID']数据库中的主键在哪里 -所以每个值都不同。

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

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

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.

这是针对收件箱系统的,我在下面有一个按钮,我希望(单击时)删除所有$row['Report ID']选中复选框的消息(ID:)。

回答by Sean Walsh

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'][]).

将表单中的名称设置为check_list[],您将能够以数组( $_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.
    }
}
?>

回答by Scone

EditTo reflect what @Marc said in the comment below.

编辑以反映@Marc 在下面的评论中所说的内容。

You can do a loop through all the posted values.

您可以遍历所有发布的值。

HTML:

HTML:

<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />

PHP:

PHP:

foreach($_POST['check_list'] as $item){
  // query to delete where item = $item
}

回答by knittl

you have to name your checkboxes accordingly:

您必须相应地命名您的复选框:

<input type="checkbox" name="check_list[]" value="…" />

you can then access all checked checkboxes with

然后您可以访问所有选中的复选框

// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
   // do something
}

ps. make sure to properly escape your output (htmlspecialchars())

附:确保正确转义您的输出 ( htmlspecialchars())

回答by Mārti?? Briedis

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

And after the post, you can loop through them:

在帖子之后,您可以循环浏览它们:

   if(!empty($_POST['check_list'])){
     foreach($_POST['check_list'] as $report_id){
        echo "$report_id was checked! ";
     }
   }

Or get a certain value posted from previous page:

或者从上一页获取某个值:

if(isset($_POST['check_list'][$report_id])){
  echo $report_id . " was checked!<br/>";
}

回答by Jon Gallup

Sorry, old topic but this is one important piece to mention for this, which @JamesRattray and others were having issues with.

抱歉,老话题,但这是一个重要的话题,@JamesRattray 和其他人遇到了问题。

Do NOT self close the inputtag (using />) when you are trying to post multiple values to the PHP script. When you self close the tag, it ends the array definition and you will only have a single value posted to your script on submission. In effect @Scone 's answer may not work until the />is changed.

当您尝试将多个值发布到 PHP 脚本时,请勿自行关闭input标记(使用/>)。当您自行关闭标签时,它会结束数组定义,并且您在提交时只会将一个值发布到您的脚本中。实际上,@Scone 的答案在/>更改之前可能不起作用。

This is what causes the Warning: Invalid argument supplied for foreach() in /home1/...PHP error in this case.

这就是Warning: Invalid argument supplied for foreach() in /home1/...在这种情况下导致PHP 错误的原因。

EDIT: As noted in the comments below, further research suggests that this is dependent on DOCTYPE. If you have the DOCTYPE set for XHTML, it will require that the input tag be closed. If you have DOCTYPE set of HTML5, it will require that the input tag not be closed. There is also a chance that this could be dictated by what browser the user is viewing the page in and how well it follows the DOCTYPE specified in the code.

编辑:如下面的评论所述,进一步的研究表明这取决于 DOCTYPE。如果您为 XHTML 设置了 DOCTYPE,则需要关闭输入标签。如果您有 HTML5 的 DOCTYPE 集,则需要不关闭输入标签。这也有可能由用户正在查看页面的浏览器以及它遵循代码中指定的 DOCTYPE 的程度决定。

回答by Richard Rodriguez

It's pretty simple. Pay attention and you'll get it right away! :)

这很简单。注意,你会马上得到它!:)

You will create a html array, which will be then sent to php array. Your html code will look like this:

您将创建一个 html 数组,然后将其发送到 php 数组。您的 html 代码将如下所示:

<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">

Where [1] [2] [3]are the IDs of your messages, meaning that you will echoyour $row['Report ID']in their place.

哪里[1] [2] [3]ID你的消息S,这意味着你将echo$row['Report ID']在自己的位置。

Then, when you submit the form, your PHP array will look like this:

然后,当您提交表单时,您的 PHP 数组将如下所示:

print_r($check_list)

print_r($check_list)

[1] => checked [3] => checked

[1] => checked [3] => checked

Depending on which were checked and which were not.

取决于哪些被检查,哪些没有。

I'm sure you can continue from this point forward.

我相信你可以从现在开始继续。

回答by Manu Burrero Sánchez

En mi caso necesito que venga en un sólo valor, por lo tanto no me vale una matriz.

En mi caso necesito que venga en un sólo valor, por lo tanto no me vale una matriz。

    // Primero paso las matrices del post a valores separados por coma.
    foreach($_POST as $n => $v) if (is_array($v)) $_POST[$n] = implode(",", $v);

Con esto me aseguro que los valores sean 1 y separados por coma. Así en MySQL puedo hacer la consulta, venga en matriz o no:

Con esto me aseguro que los valores sean 1 y separados por coma。Así en MySQL puedo hacer la Consulta, venga en matriz o no:

Where id in ($_POST["id"])