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

时间:2022-10-16 12:53:57

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:

表单的第一部分代表用户:

  • 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 Trees for a single User in the same form. I would like to send the information as a single User with an array of Trees but 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).

问题是我需要能够以相同的形式为单个用户POST多个树。我希望将信息作为单个用户发送,其中包含一组树,但这可能对表单来说太复杂了。我唯一想到的是使用javascript创建一些带有User对象和Tree对象数组的JSON消息。但是,避免使用javascript来支持更多用户(有些人关闭脚本)会很不错。

2 个解决方案

#1


107  

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'
        )
    )
)

#2


13  

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' )

#1


107  

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'
        )
    )
)

#2


13  

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' )