从没有 javascript 的 HTML 表单发布一个数组 [英] POST an array from an HTML form without javascript

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

问题描述

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

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:

  • 名字
  • 姓氏
  • 电子邮件
  • 地址

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

The second part of the form represents a Tree:

  • 水果
  • 高度

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

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]">

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

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天全站免登陆