nullgeppetto
nullgeppetto

Reputation: 569

Show pop-up message after a form is submitted until PHP commmands are executed

I would like to use a popup message (not necessarily as a new window item), just to inform the user that current page will be refreshed shortly (truly after some background php commands will have been executed.)

I don't want to freeze the execution of php though. I want this message to be shown after a form is submitted (if ($_SERVER["REQUEST_METHOD"] == "POST") { ... for as long as the execution lasts. After php commands are executed, I run echo ('<meta http-equiv="refresh" content="1;">'); to refresh page after 1 second, but I would like to inform the user that the page will be refreshed shortly and to discourage them from refreshing manually.

Thank you!

Upvotes: 0

Views: 50

Answers (1)

mahmoudayoub
mahmoudayoub

Reputation: 896

What you mentioned can be achieved through an ajax call like the following sample code. This code should be inside a seperate .js file and included in your page

// this is the id of the form
$("#idForm").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    $(".loader").show(); // Show loader or popup message

    var form = $(this);
    var url = form.attr('action');

    $.ajax({
           type: "POST",
           url: url,
           data: form.serialize(), // serializes the form's elements.
           success: function(data)
           {
               $(".loader").hide();// hide the loader/popup message after the form is submitted

               setTimeout(function(){ // wait 1 second after you get the response
                  location.reload(true); // reload the page
               }, 1000);
           }
     });
});

Upvotes: 1

Related Questions