Ajax submit multiple forms via jquery

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

so suppose I have 3 forms

<form name="form1">
<input name="input"></input>
<submit></submit>
</form>

<form name="form2">
<input name="input"></input>
<submit></submit>
</form>

<form name="form3">
<input name="input"></input>
<submit></submit>
</form>

Each of the form has its own submit button

now suppose I have another form

<form id="submitAll">
<submit value="submit all"></submit>
</form>

whose sole function is to submit all the other 3 forms simultaneously....now here are the constraints:

    • when submitAll is submitted, it has to do so using Ajax and forward the input data from all the other 3 forms to processor.php preferably via POST
    • processor.php needs to be able to distinguish between which inputs go to which form...and then process the inputs of each form separately

My question is....what is be the best way to make processor.php be able to distinguish which inputs belong to which form.....

My previous attempts was to use jquery s serialize to serialize all the 3 forms inputs, but then it would merge all the inputs into one string and notice that since the inputs in the forms have the same name, the string goes like "input=blah&input=blah&input=blah" and I can t distinguish between which input goes to which form.....

It is highly preferable to have the inputs in the forms to have the same name so that processor.php can execute smoothly

Is there away to make POST strings be passed as arrays or json or any other format so that processor.php can distinguish which input goes to which form without making the input names differ? remember it needs to do so via Ajax

thanks in advance!!!

Answers

Why don t you use naming convention like

    • name="Form1[input]"
    • name="Form2[input]"
    • name="Form3[input]"

then do regular serialize and $.post, this will help you keep the same naming convention in processor loop

  EDIT:   
<?php 
foreach($_POST as $form) {
     // $form = array( input  =>  i am here )
     processForm($form); // names are still the same for all forms
}
?>

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/4340741/ajax-submit-multiple-forms-via-jquery

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils