Reputation: 329
I have a form, and I'd like to do special processing on it before it is submitted.
I know I can catch the "submit" event using JQuery, but how do I change the values that are actually submitted from within the callback?
Upvotes: 2
Views: 2208
Reputation: 532435
A couple of options: you could replace the form fields prior to the actual submit taking place. You could use added hidden fields for this. I don't like this option particularly since you might have validation issues where you'd have to undo your changes.
You could do the post via AJAX, posting only your values, then take some appropriate action based on the result. In this case, the original post never happens. That's what I would probably do. In this way your original form stays the same and you can handle server-side validation without having to undo anything on the form.
$(function() {
$('form').submit( function() {
var $form = $(this);
var data = // ... generate your custom data ...;
$.post( $form.attr('action'), data, function(result) {
// ... do something based on result
});
return false; //cancel original submit
});
});
Upvotes: 3