php 从没有 javascript 的 HTML 表单发布一个数组

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

POST an array from an HTML form without javascript

phphtmlformspost

提问by styfle

I have a form that is a little complex and I am hoping to simplify the server-side (PHP) processing by natively POSTing an array of tuples.

我有一个有点复杂的表单,我希望通过本地 POST 元组数组来简化服务器端 (PHP) 处理。

The first part of the form represents a User:

表单的第一部分代表一个User

  • First Name
  • Last Name
  • Email
  • Address
  • etc
  • 电子邮件
  • 地址
  • 等等

The second part of the form represents a Tree:

表单的第二部分代表一棵树

  • Fruit
  • Height
  • etc
  • 水果
  • 高度
  • 等等

The problem is that I need to be able to POST multiple Treesfor a single Userin the same form. I would like to send the information as a single Userwith an array of Treesbut this might be too complex to do with a form. The only thing that comes to mind is using javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts turned off).

问题是我需要能够以相同的形式为单个用户发布多个。我想将信息作为具有数组的单个用户发送,但这可能太复杂而无法使用表单。唯一想到的是使用 javascript 创建一些带有 User 对象和 Tree 对象数组的 JSON 消息。但是最好避免使用 javascript 来支持更多用户(有些人关闭了脚本)。

回答by Joseph

check this one out.

看看这个。

<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">

<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">

<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">

<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">

it should end up like this in the $_POST[] array (PHP format for easy visualization)

它应该在 $_POST[] 数组中像这样结束(PHP 格式,便于可视化)

$_POST[] = array(
    'firstname'=>'value',
    'lastname'=>'value',
    'email'=>'value',
    'address'=>'value',
    'tree' => array(
        'tree1'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree2'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree3'=>array(
            'fruit'=>'value',
            'height'=>'value'
        )
    )
)

回答by Dan

You can also post multiple inputs with the same name and have them save into an array by adding empty square brackets to the input name like this:

您还可以发布具有相同名称的多个输入,并通过在输入名称中添加空方括号将它们保存到数组中,如下所示:

<input type="text" name="comment[]" value="comment1"/>
<input type="text" name="comment[]" value="comment2"/>
<input type="text" name="comment[]" value="comment3"/>
<input type="text" name="comment[]" value="comment4"/>

If you use php:

如果您使用 php:

print_r($_POST['comment']) 

you will get this:

你会得到这个:

Array ( [0] => 'comment1' [1] => 'comment2' [2] => 'comment3' [3] => 'comment4' )