This doesn't seem like it should be too difficult to accomplish, but I'm in a rush and I have been looking for a simple answer. I need to submit a form on page load. This is my AJAX submit function which works fine. I just need to figure out how to trigger it on page load.
Any help is much appreciated!
$("form#seller-agreement-form").submit(function() {
// we want to store the values from the form input box, then send via ajax below
$.ajax({
type: "POST",
url: "http://www2.myurl.com/formhandler",
data: "email="+ email + "&status=" + status,
success: function(){
$('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});
}
});
return false;
});
if you call $().submit()
it triggers the action; $().submit(function)
binds a handler to the submit event.
You can skip the submit however and just call the ajax method directly.
$(function() {
$.ajax({
type: "POST",
url: "http://www2.myurl.com/formhandler",
data: "email="+ email + "&status=" + status,
success: function(){
$('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});
}
});
});
To actually submit the form on page load, you would write something like
$(function() {
$('#seller-agreement-form').submit();
});
But if what you're trying to do is just to perform the same action that you would otherwise have performed when the form was submitted, then perhaps you don't want to submit the form, but only to do this:
function postForm() {
$.ajax({
type: "POST",
url: "http://www2.myurl.com/formhandler",
data: "email="+ email + "&status=" + status,
success: function(){
$('form#seller-agreement-form').hide(function(){$('div#output').fadeIn();});
}
});
}
$("form#seller-agreement-form").submit(function() {
postForm();
return false;
});
$(function() {
postForm();
});
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