I was wondering if there was a succinct way to submit a form to my backend with JQuery AJAX (async). What I mean, more specifically, is:
HTML Form:
<form method="post" action="/Intranet/Resources/Forms/WorkOrder.php">
<input type="text" name="notes[]" />
<input type="text" name="notes[]" />
...
<!-- a whole bunch more input fields here -->
</form>
This form is pretty awkward to separate into a bunch of variables with JQuery. I don't want the user to be redirected away from the page - I want the form to submit asynchronously. It's actually a save button...
Would it be better to screw it and just use JSON?
$(function(){
$("form").submit(function(){
$.ajax({
url:"save.php",
type:"post",
data:$(this).serialize(),
success: alert('saved');
});
});
});
You can use $('form').serialize() to serialize a form. The result (for your form) would be:
notes=val1¬es=val2
http://api.jquery.com/serialize/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With