0

I am a bit puzzled with this one. I am trying to display a "Please wait..." message when a form is submitted (see code below) that is running PHP on the same page. It appears to run the PHP and then reloads the page.

I have added the sleep(10) just for testing purposes.

Can anyone help?

Thanks,

John

EDIT: Ok, I now understand the difference between server and client side. So - my question is: How do I display a message when the submit is clicked and before the PHP is run?

<?php
if (isset($_POST['submit'])) {
echo "Please wait..."; //display a loading message
}
?>

<form action="test.php" method="post">
    <input type="test"></input>
    <button name="submit" type="submit">SEND</button>
</form>

<?php
if (isset($_POST['submit'])) {
sleep(10); //run a script that takes a while
}
?>
John Higgins
  • 857
  • 12
  • 25
  • I get the feeling you're not quite getting the difference between client and server side code : https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming - by the time anything is written to the screen PHP has packed up and gone home for the night basically. – CD001 Apr 18 '17 at 15:54
  • [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Jay Blanchard Apr 18 '17 at 15:57
  • @CD001 - Just reading up on that before reading your comment. Is there a work around? – John Higgins Apr 18 '17 at 15:57
  • Have a PHP script that processes the form - post to it with AJAX and have JavaScript create the "loading" modal window which sits there until the "complete" condition is met. jQuery.ajax is probably the easiest bet : http://api.jquery.com/jquery.ajax/ – CD001 Apr 18 '17 at 16:01

1 Answers1

2

Your PHP script won't be able to send the message and then redirect. You need to use JavaScript on the wait page and redirect. If you are not trying to redirect, not sure why the please wait message would be needed.

setInterval(function(){window.location.href="your_url" }, 10000);

For the download script, it needs to set the content type to PDF which means you can't send anything before that (header already sent error otherwise).

Two possible solutions:
1. You should display your 'please wait' on the page where the user clicks and open the download script in another page.
2. The download page can display the html and then use an iframe for downloading the file. Using jQuery and iFrame to Download a File

Community
  • 1
  • 1
TurtleTread
  • 1,297
  • 2
  • 12
  • 23
  • The actual PHP code is using HTML2PDF to generate a PDF which takes a few seconds. I didn't want to over complicate the post with a great line of code so just included the sleep(10) for demonstrative purposes. – John Higgins Apr 18 '17 at 16:00
  • This is what you call an XY problem xyproblem.info/. You should describe what you are trying to accomplish as the goal first. See my edits. – TurtleTread Apr 18 '17 at 16:12