Delayed form submission using JQuery

NathanJrT

I am trying to delay my form submission so that when valid information is entered an alert pops up to tell the user that their information has been recorded. The page should then wait for X seconds before submitting the form.

I have managed to get the alert pop-up working with one of my other forms but for some reason it is not working with this one.

I'll include my JQuery code below, any ideas?

Thanks

THIS IS THE CODE FOR THE FORM THAT WORKS:

$('#addprebooked_form').submit(function(e) {

		if ($('#addprebooked_email').val() != $('#addprebooked_confirmemail').val())
		{
			$("<div class='alert alert-warning' style='position: fixed; bottom: 0;'>The emails you have enter do not appear to match! Please check you have entered the visitors details correctly and try again.</div>").appendTo('#prebook').fadeOut(3000);
			e.preventDefault();
			return false;
		}
		else
		{
			$("<div class='alert alert-success' style='position: fixed; bottom: 0;'>Success! This visitor has been booked in for the time and date specified.</div>").appendTo('#prebook').fadeOut(3000);
			var delay = 3000;
			setTimeout(function () { $(this).submit(); }, delay);
		}

	});

THIS IS THE CODE I AM HAVING PROBLEMS WITH:

$('#addevent_form').submit(function(e) {

		if ($('#addevent_form').validate().checkForm() === false)
		{
			e.preventDefault();
			return false;
		}
		else
		{
			$("<div class='alert alert-success' style='position: fixed; bottom: 0;'><p>Success! This event has been scheduled for the time and date specified.</p></div>").appendTo('#createevent').fadeOut(3000);
			var delay = 3000;
			setTimeout(function () { $(this).submit(); }, delay);
		}

	});

Arun P Johny

I don't think that will work, because this inside the setTimeout() handler will not refer to the form element.

Also calling the jQuery's submit() method will enter a recursive loop because that will again call the submit event handler.

In your case, you need to prevent the form submit in the handler either way, then in the fadeOut() complete handler, you need to call the form element's submit() method.

$('#addevent_form').submit(function(e) {
  e.preventDefault();
  if ($('#addevent_form').validate().checkForm()) {
    var form = this;
    $("<div class='alert alert-success' style='position: fixed; bottom: 0;'><p>Success! This event has been scheduled for the time and date specified.</p></div>").appendTo('#createevent').fadeOut(3000, function() {
      form.submit();
    });
  }
});

Demo: Fiddle

Note: I'm not sure how the first example is working

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ajax form submission on a form created using jquery

From Dev

Using PHP form validation on jQuery form submission

From Dev

Modal form Submission to PHP using Jquery .ajax

From Dev

form submission using jquery.confirm

From Dev

Prevent double submission of form using jQuery

From Dev

Form submission using Jquery: NULL is submitted

From Dev

form submission using jquery.confirm

From Dev

Bootstrap tab Multiple form submission by using form id in jquery

From Dev

Jquery form submission is not working

From Dev

Jquery form validation and submission

From Dev

jquery form and image submission

From Dev

jquery dynamic form submission

From Dev

Form submission trouble with jQuery

From Dev

Jquery mobile form submission

From Dev

jQuery preventing form submission

From Dev

Grails Form Submission Fails with Dynamic Fields using JQuery

From Dev

Using jQuery AJAX to call a php page on form submission

From Dev

How to confirm a form submission with Bootbox using jQuery AJAX and JSON

From Dev

jQuery - stop form submission from changing page using ajax

From Dev

Grails Form Submission Fails with Dynamic Fields using JQuery

From Dev

Don't reload page after form submission using ajax and jquery

From Dev

Form Submission not working second time after submiting using Jquery and Ajax

From Dev

Jquery function BEFORE form submission

From Dev

Rails/jQuery prevent form submission

From Dev

Form values not passed by jquery submission

From Dev

Listener for form submission not working with jQuery

From Dev

jQuery $.get() is not executed on form submission

From Dev

Prevent form submission in jQuery by ID

From Dev

Pass name in jQuery form submission

Related Related

HotTag

Archive