从不带javascript的HTML表单发布数组 [英] POST an array from an HTML form without javascript

查看:124
本文介绍了从不带javascript的HTML表单发布数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



表单的第一部分代表用户




  • 名字


  • 电子邮件

  • 地址

  • etc

>

表单的第二部分表示一个


  • 水果

  • 身高

  • 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
  • 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).

解决方案

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[] = 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆