tags:

views:

135

answers:

2

I have a page on which user can dinamically create some identical groups of inputs, fill it and send to server.

<input type="text" name="firstName"/>  
<input type="text" name="lastName"/>

What is a preferable way of sending this data to server?

Maybe there are some simple ways of emulating hierarhical data over POST request, avoiding XML structures? Different "name" attribute values ("firstName1", "firstname2")? Creating ID for each group or input? Relying on the order of name-value pairs in the POST request?

EDIT: of course i know about JSON. But just to use some minimal hierarchies i would like to follow the second answer: rely on the order of firstname-lastname in post request.

+3  A: 

You could use JSON serialization instead of XML. On the browser side you could use one of the Javascript JSON libraries like this one and there's plenty of implementations for server side languages, which you can use for deserialization.

axk
i know about json of course. i just looked for simpler decision for little hierarchies.
rudnev
A: 

If the data pairs are ordered, you don’t need additional identifiers as the order already identifies the data pairs. So you could just use firstName and lastName for every data pair:

firstName=First%20Name%201&lastName=Last%02Name%201&firstName=First%20Name%202&lastName=Last%20Name%202&…

You server-side application then just combines each data pair.

But some languages/systems already do this when the data has a special format. In PHP you could use the arg[] syntax to automatically get an array of the data.

Gumbo
well, i use .net, which is using NameValueCollection for Request.Form, which gets values comma-separated this way:Request.Form["firstName"] // firstName1,firstname2,firstname3it does not take position of each value, so firstName and lastname will not be combined. (or am i missing anything?)
rudnev
I don’t know .NET. But have you tried the `index` parameter? (See http://msdn.microsoft.com/library/ms525985.aspx)
Gumbo
it works if i always have a firstname and lastname, yes. it doesn't work if i have optional parameters.
rudnev
What exactly do you mean by that?
Gumbo
well if some fields are optional and can be missing - i will have two non-equal lists of values e.g "firstname1,firstname2,firstname3" and "lastname1,lastname2" - and i will not be able to combine firstname with appropriate lastname.
rudnev
Gumbo
i understand, thanks:)). i would love to find out are there any less obvious beatiful decisions. it seems there are no.
rudnev