Server-side dynamic form processing

I saw a lot of questions about implementing dynamic forms in jQuery or other javascript libraries, and it seems to me that I managed to get up and run the dynamic form setup for my testing purposes.

My question is what is best for designating form fields and processing them on the server side.

I am trying to implement a contact form in which a user can add several phone numbers (and types), as well as several addresses (and types), something similar to the code below, this is a block of code 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>

Then I will have a +/- link or button to add another phone or delete the phone. When I submit a form, what is the best way to handle the name / type combination Should I have the names above with a postfix id, for example phone1 / type1 , or use an array name, for example phone [] / type [] , and match the pairs to server according to the index.

I use java (not sure if it matters if it is java or php or something else), but what would be the best thing to do this.

thank

+3
source share
2 answers

, -, , , . Java, , , , , Javascript-.

Java, , , - Stripes, . bean

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

, " [0].street1", " [0].street2" .. , "1" "0".

Java -.

+1

. , .

, , . .

, . :

<div id="phones">
<input type-"hidden" name="count" value="3" />
<ul>

<li>
<label>Phone Number</label><input type="text" name="phone1" value="" />
<label>Type</label><input type="text" name="type1" value="" />
</li>
<li>
<label>Phone Number</label><input type="text" name="phone2" value="" />
<label>Type</label><input type="text" name="type2" value="" />
</li>
<li>
<label>Phone Number</label><input type="text" name="phone3" value="" />
<label>Type</label><input type="text" name="type3" value="" />
</li>
</ul>
</div>
0

Source: https://habr.com/ru/post/1756419/


All Articles