PDA

View Full Version : Submit multiple forms using single AjaxRequest



AndreaCammarata
4 Sep 2010, 5:31 AM
Hi guys,
I'm going to ask you something stupid because I think someone ask you one thousand times but...
How to submit multiple forms using a single AjaxRequest?

I tried the following code but i dosn't success.



var dbParams = Ext.encode(Ext.getCmp('INDB-form').getForm().getValues());
var adminParams = Ext.encode(Ext.getCmp('INAD-form').getForm().getValues());
var netParams = Ext.encode(Ext.getCmp('NET-form').getForm().getValues());

Ext.Ajax.request({
method: 'post',
url: 'setup.php',
params: [
dbParams,
adminParams,
netParams
],
success: function(response, opts) {
if (response.responseText == 'undefined') {


}
}
});
Does someone has a quick trick to do it?
Thanks a lot for your help!

moegal
4 Sep 2010, 6:02 AM
I think getValues will just return an array of the values and not the keys. Try getFieldValues() instead. An maybe combine your three vars instead of listing them as properties(maybe you need to use apply, not sure). I have not tested this, just a suggestion.



params: [
dbParams+adminParams+netParams
],

AndreaCammarata
4 Sep 2010, 6:33 AM
Hi moegal and thanks for your reply but it doesn't work.
I tried your suggestions but nothing.
Sure i could write down the code in the following way:



var a = Ext.getCmp('fieldA').getValue();
var b = Ext.getCmp('fieldB').getValue();
var c = Ext.getCmp('fieldC').getValue();

Ext.Ajax.request({
method: 'post',
url: 'setup.php',
params: [
'a': a,
'b': b,
'c': c
],
success: function(response, opts) {
if (response.responseText == 'undefined') {


}
}
});


But there must be a faster way to do the same.
Any other suggestions?

moegal
4 Sep 2010, 6:56 AM
what do you get when you do this?



var myparams = Ext.getCmp('INDB-form').getForm().getFieldValues();
var myparams = Ext.applyIf(myparams, Ext.getCmp('INAD-form').getForm().getFieldValues());
var myparams = Ext.applyIf(myparams, Ext.getCmp('NET-form').getForm().getFieldValues());

console.log(Ext.encode(myparams))

AndreaCammarata
4 Sep 2010, 8:55 AM
If I write the code you post i get all the params inside a json string



{"a":"a value", "b":"b value", "c":"c value"}


that is exact what I wanted, so I try to write the following code:



var myparams = Ext.getCmp('INDB-form').getForm().getFieldValues();
var myparams = Ext.applyIf(myparams, Ext.getCmp('INAD-form').getForm().getFieldValues());
var myparams = Ext.applyIf(myparams, Ext.getCmp('NET-form').getForm().getFieldValues());

Ext.Ajax.request({
method: 'post',
url: 'setup.php',
params: [
Ext.encode(myparams)
],
success: function(response, opts) {
if (response.responseText == 'undefined') {


}
}
});


but if I open firebug to see what is send over the server that is what i see as my params



{"a":"a value", "b":"b value", "c":"c value"}


and not as i expect



a :value a
b :value b
c :value c


in fact if, server side i try to write the value of param a



echo $_POST["a"];


I got the following error



Notice: Undefined index: a in setup.php on line 13


so the params are not send correctly to the server.

moegal
4 Sep 2010, 8:56 AM
Have you tried just looping thru each form field and creating your own param list?

AndreaCammarata
4 Sep 2010, 8:59 AM
Do you have a quick example on how to do it?

Animal
4 Sep 2010, 10:09 AM
Why don't you just have one form, which contains Containers? Just submit the form.

AndreaCammarata
4 Sep 2010, 10:24 AM
Why don't you just have one form, which contains Containers? Just submit the form.
Hi Animal, my page has a wizard like structure, so every step has it's own form.
After some validation I have to send every step param inside a single AjaxCall without "bounce" from server and client making a request for every step form.
Do you have any suggest?

Animal
4 Sep 2010, 10:41 AM
No, every step has its own Container

The whole is encapsulated in a FormPanel

moegal
4 Sep 2010, 10:43 AM
I don't have an example handy, should be something in the forum.