-3

I'm trying to submit 2 symfony forms in one submit using ajax

var data = $('#facebookPostTargetForm').serialize();
data = data.concat("\r\n", $('#facebookPostForm').serialize());
$.ajax({
    url: '{{ path('facebookpostwizard_new') }}',
    type: "POST",
    data: data,
    dataType: 'html',
    success: function(data){
    //console.log(data);
    alert('OK');
    }
    });

almost everything is working properly except the case that in my controller when I use $form->handleRequest($request) i get some trouble getting the last field of first form(mostly _token so $form->isValid() always return false) and first field of second one!!!! I don't put a snippet of my controller here because if I only submit one form everything works perfect, it seems to be related to the data.concat() but I've tried a lot of things and it doesn't work. Thanks in advance!

  • 3
    "some trouble" and "doesn't work" means what, exactly? Descriptions of error messages and/or unexpected behaviour would be more appropriate than these vague statements. But...why are you adding line breaks in front of your serialised form data??? What is that for? I doubt the server is going to understand what you're trying to submit. – ADyson Dec 18 '17 at 22:13

1 Answers1

2

Without knowing your PHP code, I can only guess what the issue is. But I suspect you are not handling the Array that you receive correctly. Or rather, you are trying to build something totally different from what you are actually sending.

concat() does not concatenate strings but Arrays. If you just want to concatenate the serialized query strings of the forms, you can simply use + operator like this:

var data = $('#facebookPostTargetForm').serialize() + '&' + $('#facebookPostForm').serialize();

This will make one big query string from both forms and you will have them available as POST variables in the $request object. Note that this will only work if the fields in both forms are named differently. If a field has the same name, form2 will override form1.

If you use the same field names and want to handle the forms separately, you should post an object instead of a form:

var data = {
    'form1': $('#facebookPostTargetForm').serializeArray(),
    'form2': $('#facebookPostForm').serializeArray()
};

This will submit an object ob objects (or a multidimensional associative array in PHP speech). You can then access them in PHP like this:

$form1 = $request->get('form1');
$field1 = $form1['field1']
masterfloda
  • 2,908
  • 1
  • 16
  • 27
  • good point @masterfloda I deleted my post since I noticed it is redundat – Juan I. Morales Pestana Dec 19 '17 at 18:51
  • Hi it was very usefull, just to say that I use concat in strings based on this https://www.w3schools.com/js/js_string_methods.asp, I was just missing the fact of using '&' in concat and it's working like a charm!! Ithink I can't mark this as an answer and it's a shame!!! Thanks anyway!!!! – Rubislandy Rodriguez Dec 20 '17 at 14:22
  • @masterfloda is there a similar way to "var data = $('#facebookPostTargetForm').serialize() + '&' + $('#facebookPostForm').serialize();" but uploading files in one of related form???? Thanks in advance!!! – Rubislandy Rodriguez Jan 08 '18 at 20:55
  • @RubislandyRodriguez Not with plain jQuery. For this, Juan's deleted answer would have been helpful :-) The solution is the ajaxForm plugin. See this answer: https://stackoverflow.com/a/4545089/7933618 – masterfloda Jan 09 '18 at 01:13
  • @masterfloda I found this: var target = $('#facebookPostTargetForm').serializeArray(); var data = new FormData($('#facebookPostForm')[0]); for (var i=0; i – Rubislandy Rodriguez Jan 16 '18 at 22:07