views:

36

answers:

1

Hi, first a little bit of documentation from the jQuery validation plugin:

"Use submitHandler to process something and then using the default submit. Note that "form" refers to a DOM element, this way the validation isn't triggered again."

submitHandler: function(form) {
    $.ajax({
        type: 'POST',
        url: form.action,
        data: 'current_password=' + form.current_password + '&new_password=' + form.new_password,
        success: function(){
            alert("succes");
        }
    });
}

So, naturally my ingenious piece of code isn't working. I'm trying to access the 'action' attribute and the two input fields from the form object, with no luck. How am I supposed to do this?

+2  A: 

Try this instead:

submitHandler: function(form) {
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: { current_password : form.current_password.value,
                new_password: form.new_password.value }
        success: function(){
            alert("succes");
        }
    });
}

Currently instead of submitting the values of the elements it's literally calling a toString on them, so just add .value to get the values. Also we're passing data as an object here so it gets encoded, otherwise if for example the password had a & in it, the post wouldn't be correct.

Nick Craver
The data object works great, but I still get an error when trying to access form.action. Is there a way to call this attribute from the object?
soren.qvist
@soren.qvist - What does your `<form>` element look like? Also is `$(form).attr('action')` giving a different result?
Nick Craver
$(form).attr('action') works fine. But I'm worried that this approach is recursive, ie. selecting the form again instead of using the current object.
soren.qvist
@soren.qvist - oh no, it's fine to do that, you just don't want to do `$(form).submit()` specifically, since that ends up calling `submitHandler` again :)
Nick Craver
Oh okay, well I guess it works out fine then! Thank you very much for the help.
soren.qvist