Skip to content Skip to sidebar Skip to footer

How To Submit A Form Via Ajax Using A Different Action?

I have a form element and that has a normal submit button but I also want to be able to submit an AJAX get request using the values entered in the form but make it to a different a

Solution 1:

This will be easiest if you use a library (Prototype, jQuery, Closure, or any of several others). With the help of such a library, it becomes almost trivially easy to do this.

Prototype example:

$('theFormElementId').request({
    onSuccess: function(response) {
        // GET was successful
    }
});

that uses Prototype's Form#request, but you could use Ajax.Request instead and get the form data via Form#serialize (Form#request basically just does that for you).

jQuery example:

$.get("yoururl", $('#theFormElementId').serialize(), function(data) {
    // The GET was successful
});

That uses jQuery's $.get and serialize functions (you could use $.ajax instead of $.get for more control/options).

It's totally possible to do this without a library, it's just a lot more work.

Post a Comment for "How To Submit A Form Via Ajax Using A Different Action?"