views:

358

answers:

2

I have a form with name orderproductForm and an undefined number of inputs.

I want to do some kind of jQuery.get or ajax or anything like that that would call a page through Ajax, and send along all the inputs of the form orderproductForm.

I suppose one way would be to do something like

jQuery.get("myurl",
          {action : document.orderproductForm.action.value,
    cartproductid : document.orderproductForm.cartproductid.value,
    productid : document.orderproductForm.productid.value,
       ...

However I do not know exactly all the form inputs. Is there a feature, function or something that would just send ALL the form inputs?

Thanks

+3  A: 

serialize or ajaxForm

$("#theForm").ajaxForm({url: 'server.php', type: 'post'})

ajaxForm will send when the submit button is pressed. ajaxSubmit sends immediately:

$("#theForm").ajaxSubmit({url: 'server.php', type: 'post'})

another way:

$.get('server.php?' + $('#theForm').serialize())

$.post('server.php', $('#theform').serialize())

http://docs.jquery.com/Ajax/serialize

jspcal
interesting idea, however, i do not control the server side I am calling, therefore I cannot send it serialized data
nute
Yes you can, the name isn't important what it will do is send pairs of every form-key and every form-variable.
Steve Kemp
It's serialized into a query string, just like the data you are putting into the array manually in the GET call there would be. This is what you want, I'm pretty sure.
Alex JL
oooh I see, I then add this string to the end of the URL in the get call. I was thinking PHP serialize. Sorry. Thanks!
nute
A: 

There's also the submit event, which can be triggered like this $("#form_id").submit(). You'd use this method if the form is well represented in HTML already. You'd just read in the page, populate the form inputs with stuff, then call .submit(). It'll use the method and action defined in the form's declaration, so you don't need to copy it into your javascript.

http://docs.jquery.com/Events/submit#examples

billw