1

I'm trying to figure out how to submit my form with a page refresh. I'm creating a small dealer locator for a client, and when my form submits, they update the dealer list fine. What I'm trying to do is have it so the page doesn't reload. So far I've written the following code, but not sure how to post the variable and have it take effect by my Ruby on Rails code.

$("#getDealers ").submit(function(e) {
    e.preventDefault();

    // pull in postal code variable


});

1 Answer 1

3

You could use the .serialize() method to send the contents of the form using AJAX to the corresponding action as if it was normal request:

$('#getDealers').submit(function(e) {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function(result) {
            // TODO: do something with the result returned by the server
        }
    });
    e.preventDefault();
});

And to avoid writing all this code you could use the excellent jquery form plugin:

$(function() {
    // AJAXify the getDealers form
    $('#getDealers').ajaxForm(function(result) {
        // TODO: do something with the result returned by the server
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.