tags:

views:

472

answers:

1

I am using jQuery in conjunction with Symfony 1.3.2.

I have registered the form's submit button click event with jQuery, so that jQuery POSTs to the correct action at the server.

My code looks like this:

$(document).ready(function(){
  $('#form_btn').click(function(){
     $.ajax( {'type': 'POST', 'url': '/form_handler.php' });
  });
});

However (perhaps unsurprisingly - since I did not send any data), at the server end, there are no POST variables and the form fields are not available.

For example:

$request->getParameter($this->form->getName());

returns null.

Does anyone know how I can POST a Symfony form using jQuery?

[Edit]

For those who may not be familiar with Symfony, what I am asking here is how to post form values using jQuery.

+1  A: 

You have to add all the input fields from the form to the .ajax() call, something like

$.ajax({'type': 'POST', 'url': '...', 'data': form_data });

Where var form_data are the values of your input elements - see here on how to get them: http://stackoverflow.com/questions/169506/get-form-input-fields-with-jquery

matix
@matix: +1 .thanks for the quick response. I'm reading up the link you sent now
morpheous
var form_data = $('#myForm').serialize();
SeanJA
Yes, you should use serialize() ... and an alternative in Symfony 1.3/1.4 is to use the sfJQueryReloadedPlugin which has a submit_to_remote method that handles Ajax form submission. I personally prefer writing it myself, as you're doing now. The plugin comes with some overhead.
Tom