0

I have a jQuery function that I want to run after a form is successfully submitted. The function fades in a screen overflow then displays items in a list before redirecting to another url.

Here's the script:

    <script >
      function startCheck() {
    var e = $(".overlay-checker"),
        t = $(".overlay-checker-points > li");
    for (t.hide(), e.fadeIn(), i = 0; i < t.length; i++) setTimeout(function() {
        $(".overlay-checker-points").find(":hidden").first().fadeIn()
    }, 1500 * (i + 1));
    setTimeout(function() {
        window.location = $(".redirectNow").attr("href")
    }, 1500 * t.length + 2e3)
    }
   </script>

How do I run the above function after a form is successfully submitted?

2
  • Maybe by using the submit event? How do you run it now? Post a minimal reproducible example please Commented Apr 13, 2020 at 1:24
  • I don't have enough knowledge of jQuery to formulate anything close to a working example. Commented Apr 13, 2020 at 1:47

1 Answer 1

1

You may use the following listener:

$(document).on("submit", "form", function(e){
   // Prevent form submission
   e.preventDefault();
   // Show loader
   // Optional, but showing that something's happening is probably good
   // Assuming you have some element with class "loader" hidden...
   $(".loader").show();
   // Submit form via ajax call
   $.ajax({
      url: "path_to_your_file.php",
      type: "post",
      data: $("form").serialize(),
      success: function(data){
         // Form submitted
         // Call script here 
      },
      error: function(data){
         // Handle error here
      },
      complete: function(){
         // Will be called after success or error
         // Hide loader (if shown before ajax call above)
         $(".loader").hide();
      }
   });
})

And on your server side, you can retrieve your form's inputs using $_POST["name"]

EDIT:

This is what your HTML form would look like:

<form>
   <input type="email" name="useremail" placeholder="Email address" required>
   <!-- Add any other input, select, textarea of your liking -->
   <button type="submit">Submit</button>
</form>

You'll retrieve the input in your PHP file like this:

$email = $_POST["useremail"];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Will. If I follow your example how do I format the form html? What about the action attribute?
You can leave out the action attribute since the form will be submitted via ajax. I'm updating my response to show you how to put that form together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.