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
  • You should have that on the form.submit action rather than the button.click of the button so if they hit enter in one of the inputs it will still work. Commented May 12, 2010 at 15:27

1 Answer 1

1

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: Obtain form input fields using jQuery?

An example would be

var form_data = $('#myForm').serialize();
Sign up to request clarification or add additional context in comments.

3 Comments

+1 .thanks for the quick response. I'm reading up the link you sent now
var form_data = $('#myForm').serialize();
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.