submit form using jquery code example
Example 1: jQuery AJAX form submit
// jQuery ajax form submit example, runs when form is submitted
$("#myFormID").submit(function(e) {
e.preventDefault(); // prevent actual form submit
var form = $(this);
var url = form.attr('action'); //get submit url [replace url here if desired]
$.ajax({
type: "POST",
url: url,
data: form.serialize(), // serializes form input
success: function(data){
console.log(data);
}
});
});
Example 2: jquery submit form
// It is simply
$('form').submit();
// However, you're most likely wanting to operate on the form data
// So you will have to do something like the following...
$('form').submit(function(e){
// Stop the form submitting
e.preventDefault();
// Do whatever it is you wish to do
//...
// Now submit it
// Don't use $(this).submit() FFS!
// You'll never leave this function & smash the call stack! :D
e.currentTarget.submit();
});
Example 3: put form action jquery
$('#button1').click(function(){
$('#formId').attr('action', 'page1');
});
$('#button2').click(function(){
$('#formId').attr('action', 'page2');
});
Example 4: when a form is subbmited jquery
$("#myform").submit(function(e) {
})
Example 5: jquery submit form
<input type='button' value='Submit form' onClick='submitDetailsForm()' />
<script language="javascript" type="text/javascript">
function submitDetailsForm() {
$("#formId").submit();
}
</script>
Example 6: ajax post form listener button
$("button").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/pages/test/",
data: {
id: $(this).val(), // < note use of 'this' here
access_token: $("#access_token").val()
},
success: function(result) {
alert('ok');
},
error: function(result) {
alert('error');
}
});
});