从不带javascript的HTML表单发布数组 [英] POST an array from an HTML form without javascript
问题描述
我有一个有点复杂的表单,我希望通过本地化发布元组数组来简化服务器端(PHP)处理过程。
表单的第一部分代表用户:
- 名字
- 姓
- 电子邮件
- 地址
- etc
表单的第二部分表示一个树:
问题是我需要能够以相同的形式为单个用户发布多个树。我希望将这些信息作为一个用户发送给树的数组,但是这对于表单来说可能过于复杂。唯一想到的就是使用javascript来创建一些带有User对象和Tree对象数组的JSON消息。但是,避免使用javascript来支持更多的用户(有些人关闭了脚本)是很好的做法。
检查一下。 < input type =textname =firstname>
< input type =textname =lastname>
< input type =textname =email>
< input type =textname =address>
< input type =textname =tree [tree1] [fruit]>
< input type =textname =tree [tree1] [height]>
< input type =textname =tree [tree2] [fruit]>
< input type =textname =tree [tree2] [height]>
< input type =textname =tree [tree3] [fruit]>
< input type =textname =tree [tree3] [height]>
它应该在$ _POST []数组(PHP格式,便于可视化)中像这样结束
$ _ POST [] = array(
'firstname'=>'value',
'lastname '=>'value',
'email'=>'value',
'address'=>'value',
'tree'=> array(
'tree1'=> array(
'fruit'=>'value',
'height'=>'value'
),
'tree2' =>数组(
'fruit'=>'value',
'height'=>'value'
),
'tree3'=> array(
'fruit'=>'value',
'height'=>'value'
)
)
)
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.
The first part of the form represents a User:
- First Name
- Last Name
- 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).
<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[] = 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'
)
)
)
这篇关于从不带javascript的HTML表单发布数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!