在服务器端处理动态表单 [英] Processing Dynamic forms on the Server Side

查看:120
本文介绍了在服务器端处理动态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了很多有关在jQuery或其他javascript库中实现动态表格的问题,而且我认为我已经成功建立并运行了用于测试目的的动态表格.

I've seen a lot of questions around here about implementing dynamic forms in jQuery or other javascript libraries, and I think I managed to get up and running in setting up a dynamic form for my test purposes.

我的问题是命名表单字段并在服务器端处理它们的最佳实践是什么.

My question is whats the best practice in naming my form fields and processing them on the server side.

我正在尝试实现类似联系人的表单,用户可以在其中添加多个电话号码(和类型)以及多个地址(和类型),类似于下面的代码,这是将动态复制的代码块

I am trying to implement a contact like form where the user can add multiple phone numbers (and types) as well as multiple addresses (and types) something similar to the code below, this is the code block that will be duplicated dynamically.

<div id="phones">
<label>Phone Number</label><input type="text" name="phone1" value="" />
<label>Type</label><input type="text" name="type1" value="" />
</div>

然后,我将有一个+/-链接或按钮以添加其他电话或删除电话.当我提交表单时,处理名称/类型组合的最佳方法是什么 我应该使用上面标有后缀的名称,其后缀为 phone1/type1 ,还是应该使用数组命名方式,例如 phone []/type [] ,并匹配根据索引在服务器上配对.

Then I will have a +/- link or button to add another phone or remove a phone. When I submit the form, whats the best way to handle the combo of name/type Should I have the names like indicated above with a postfix of an id like phone1 / type1 or should I use the array naming like phone[] / type[] and match the pairs on the server according to the index.

我正在使用Java(不确定是Java,PHP还是其他东西,是否会有所不同),但是这样做的最佳实践是什么.

I am using java (not sure if it makes a difference if it is java or php or whatever) but what would be a best practice of doing this.

谢谢

推荐答案

带索引的方括号似乎是大多数框架所期望的,但它确实完全取决于您的框架.在Java世界中,鉴于存在大约一百万个不同的框架,因此您必须从框架的期望入手,并适当地调整Javascript代码.

Square brackets with indexes seem to be what most frameworks expect, but it does completely depend on your framework. In the Java world, given that there are about a million different frameworks, you have to start from what your framework expects, and adapt your Javascript code appropriately.

我唯一熟悉的Java框架要知道答案是Stripes,它需要方括号.如果您的豆具有属性

The only Java framework I'm familiar enough with to know the answer is Stripes, and it would want square brackets. If your bean had a property

private List<Address> addresses;
public List<Address> getAddresses() { return addresses; }
public void setAddresses(final List<Addresses>) { this.addresses = addresses; }

然后,输入将需要使用诸如"addresses [0] .street1","addresses [0] .street2"等名称.当您为新地址添加新块时,您将拥有与"1"而不是"0".

then the inputs would need names like "addresses[0].street1", "addresses[0].street2", etc. When you add a new block for a new address, you'd have the same fields with "1" instead of "0".

但是,不同的Java框架可能以完全不同的方式来做事.

A different Java framework, however, might do things in completely different ways.

这篇关于在服务器端处理动态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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