Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多个复选框中获取$\u POST_Php_Html_Forms_Checkbox - Fatal编程技术网

Php 从多个复选框中获取$\u POST

Php 从多个复选框中获取$\u POST,php,html,forms,checkbox,Php,Html,Forms,Checkbox,我有一个表单,其中有多个复选框(每个复选框都有代码): 将表单中的名称设置为check\u list[],您将能够以数组的形式访问所有复选框($\u POST['check\u list'][]) 以下是所要求的一个小样本: <form action="test.php" method="post"> <input type="checkbox" name="check_list[]" value="value 1"> <input type="ch

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


将表单中的名称设置为
check\u list[]
,您将能够以数组的形式访问所有复选框(
$\u POST['check\u list'][]

以下是所要求的一个小样本:

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

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

if(isset($_POST['check_list'][$report_id])){
  echo $report_id . " was checked!<br/>";
}
if(isset($\u POST['check\u list'][$report\u id])){
echo$report_id.“已检查!
”; }
您必须相应地命名复选框:

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

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

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

您将创建一个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">

其中
[1][2][3]
是您的消息的
ID
s,这意味着您将
echo
您的
$row['Report ID']

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

<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">
print\r($check\u list)

[1]=>已选中
[3] =>已选中

取决于检查的和未检查的


我相信你可以从这一点开始继续。

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

您可以对所有发布的值进行循环

HTML:


你能给我举一个echo(ing)one check_list[]checkbox的例子吗?(对于所选的两个,它会相似吗?)谢谢。我还应该指定,如果HTML中没有设置值,
$check
将等于
on
如果($\u POST)
-无用,
$\u POST
将始终为真。您应该检查
!空($_POST['check_list'])
。@MārtiņšBriedis,如果数组索引不存在,则会导致数组索引越界。更好的检查方法是
array\u key\u exists('check\u list',$\u POST)&&!空($\u POST['check\u list'])
@Tyzoid
empty()
不会导致此错误。根据手册:
empty()不会在变量不存在时生成警告奇怪错误:警告:为第30行/home1/mountgam/public\u html/zombiewrath/reports.php中的foreach()提供的参数无效=/只有在字段定义中使用
[]
语法时才有效,这使得php将$\u POST值创建为数组。否则它将是单个非数组值,导致foreach()循环崩溃。我尝试了这两种方法,但好的,我会再试一次(在表单/名称中没有[])。请记住,
check\u list[]
将以
check\u list[0]
开头,而不是
check\u list[1]
。value=“checked”会起反作用。如果未指定任何值,您将获得“on”。在value属性中指定要提交的值的优点是,您可以发送几乎任意的数据,而不必成为PHP数组索引。使用foreach迭代数组值也比迭代键容易。
<input type="checkbox" name="check_list[]" value="…" />
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
   // do something
}
<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">
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
foreach($_POST['check_list'] as $item){
  // query to delete where item = $item
}