php 来自 html 表单的 $_POST 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11676011/
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 01:51:01  来源:igfitidea点击:

$_POST Array from html form

php

提问by James Stafford

I am trying to send data from multiple checkboxes (id[]) and create an array "info" in php to allow me to run a script for each value (however the quantity of values may change each time) however first I am trying to display the content of each array value. I am not quite sure how to put my array populating line to save all the content to the array.

我正在尝试从多个复选框(id [])发送数据并在 php 中创建一个数组“信息”以允许我为每个值运行一个脚本(但是每次值的数量可能会改变)但是首先我试图显示每个数组值的内容。我不太确定如何放置我的数组填充行以将所有内容保存到数组中。

HTML

HTML

echo("<input name='id[]' type='checkbox' value='".$shopnumb."'>");

my hopeful processing code currently is -

我目前希望的处理代码是 -

$info=$_POST['id[]'];
Echo(array_values($info));

what do I need to do to make the content sent by post from the form checkboxes populate the array info

我需要做什么才能使表单复选框中的 post 发送的内容填充数组信息

any help is greatly appreciated

任何帮助是极大的赞赏

edited for clarification.

编辑以澄清。

回答by Ross Smith II

Change

改变

$info=$_POST['id[]'];

to

$info=$_POST['id'];

by adding []to the end of your form field names, PHP will automatically convert these variables into arrays.

通过添加[]到表单字段名称的末尾,PHP 会自动将这些变量转换为数组。

回答by Fanda

You should get the array like in $_POST['id']. So you should be able to do this:

你应该像 $_POST['id'] 那样得到数组。所以你应该能够做到这一点:

foreach ($_POST['id'] as $key => $value) {
    echo $value . "<br />";
}

Input names should be same:

输入名称应该相同:

<input name='id[]' type='checkbox' value='1'>
<input name='id[]' type='checkbox' value='2'>
...

回答by Johndave Decano

<input name='id[]' type='checkbox' value='".$shopnumb."\'>
<input name='id[]' type='checkbox' value='".$shopnumb."\'>
<input name='id[]' type='checkbox' value='".$shopnumb."\'>


$id = implode(',',$_POST['id']);
echo $id

you cannot echo an array because it will just print out Array. If you wanna print out an array use print_r.

您不能回显数组,因为它只会打印出数组。如果你想打印出一个数组,请使用print_r.

print_r($_POST['id']);

回答by NotGaeL

I don't know if I understand your question, but maybe:

我不知道我是否理解你的问题,但也许:

foreach ($_POST as $id=>$value)
    if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];

would help

有助于

That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...

也就是说,如果您真的想在 html 表单的每个复选框上使用不同的名称 (id[key])(效率不高)。如果不是,您可以将它们全部命名为相同,即 'id' 并迭代数组的(选定的)值,例如:foreach ($_POST['id'] as $key=>$value)...