tags:

views:

40

answers:

3

I'm seeking advice on best practice when submitting a varying number of POST variables.

Do I use JSON or some character separated list to merge all the values into one field or use a sequence of fields like 'autocomplete1', 'autocomplete2' and so on.

Does jQuery have a json encoder?

Thanks in advance, Ben

A: 

To answer the last part of your question, yes, there is a json plugin for jQuery which I have used successfully, the project page is at http://code.google.com/p/jquery-json/

Carson63000
+1  A: 

It depends on what you are doing with the data you eventually merge. We often use a form field array (ex. < input type="text" name="fieldName[]" > < input type="checkbox" name="fieldName[]" value="true" >). This will put all your form fields into an array when accessing after a submit. You can then use a server-side language like PHP to parse the array and do whatever you want with it.

If you need separate field names, you could always name the form fields with separate names and then merge them server-side with something like:

foreach( $_POST as $p=>$v )
{
   $arrayFields[$p] = $v;
}

If this is strictly client-side, I would just increment the needed fields and have a standard field name, followed by "_1" , "_2", etc. You could easily merge them client-side or server side as needed.

I am not sure about JSON capabilities that would help here.

Jonathon
I didn't know php could interpret field names like this[] as an array. Thanks
Keyo
+1  A: 

To submit various post variables to the server the best way would be to post them as independent parameters. A request like this should do the trick:

http://server/automcompleter?autocomplete1=value1&amp;autocomplete2=value2&amp;...

In fact if you can create a multi-value parameter like this:

http://server/automcompleter?autocomplete=value1&amp;autocomplete=value2&amp;...

Where the parameters autocomplete would eventually be an array. If you are using php the parameter needs to be caller autocomplete[].

As for the return use json.

You can use the jQuery UI 1.8 plugin to do autocomplete.

rmarimon