-2

When I click on the submit button, I want a delay of 3 seconds and then a redirect. My form will redirect to an other PHP site.

My code:

<button
    class="button actionContinue scTrack:unifiedlogin-login-submit"
    type="submit"
    id="btnLogin"
    name="btnLogin"
    value="Login"
    pa-marked="1">
    Einloggen
</button>
</div></div>
<input type="hidden" name="" value="">
</form>

<?php
    if (isset($_POST['submit']))
    {
        sleep(3);
        // AND NOW action;
    }
?>

Example

<?php
    if (isset($_POST['submit']))
    {
        sleep(3);
        // AND NOW action;

        redirect('Location:next page.php');     
    }
?>

Why is it not working?

James
  • 4,644
  • 5
  • 37
  • 48
anit234
  • 1
  • 1
  • if you put an action after the sleep, does it wait for 3 seconds before performing the action? Like a simple echo. Try increasing it for testing purposes, like to 10 seconds, as page load times might be slow. Also, be careful adding sleep to code. It may be useful in some cases, but we tend to strive for fast page load times without delays. – James Aug 07 '18 at 22:10
  • Yes right thats i want – anit234 Aug 07 '18 at 22:10
  • change sleep to `10` and after it put `echo "whatever";`. what happens? – James Aug 07 '18 at 22:11
  • Nothing, instantly redirect – anit234 Aug 07 '18 at 22:13
  • Can you check if my example code i have posted is correct? also i have deleted in form action="" the page – anit234 Aug 07 '18 at 22:18
  • comment out or remove the redirect as it makes testing basic things harder. on your check for if `$_POST['submit']` is set, add an else and echo "not set". – James Aug 07 '18 at 22:26
  • Not any results.. – anit234 Aug 07 '18 at 22:28
  • it does nothing at all? it will echo one thing or the other, logic dictates it has to# – James Aug 07 '18 at 22:28
  • in your ` – James Aug 07 '18 at 22:29
  • I get the it is NOT set message under the button after i visit my page – anit234 Aug 07 '18 at 22:35
  • 1
    PHP is server side script with everything run by the time it gets to the browser. Your script is telling the server to wait 3 seconds and then continue, then once everything is done it will be pushed to the browser where it just sees the redirect and redirects. If you change it to sleep for 10 seconds it will take longer for the server to process the script but it will still redirect straight away. I would say that your options are to either use a JavaScript redirect (`window.location.href = '...'`) with a setTimeout or a meta tag (``). – qooplmao Aug 07 '18 at 22:52
  • You are running `sleep` on server-side. You must run it on client-side (Javascript). More info: https://stackoverflow.com/a/8133775/161640 – Isaac Aug 08 '18 at 03:11

1 Answers1

1

So the basic debugging in the comments shows that the $_POST['submit'] value which you expected to be there is actually not present. This is why your sleep is not working as the condition in your IF is not true.

So the next step is debug why you don't have that POST data. Which is because you don't have that as a form element. You will have btnLogin because that is the name you provided in your button.

name="btnLogin"

You can either test for the name you currently have:

isset($_POST['btnLogin']))

Or change the name in your button to Submit:

<button
    class="button actionContinue scTrack:unifiedlogin-login-submit"
    type="submit"
    id="btnLogin"
    name="Submit"
    value="Login"
    pa-marked="1">
    Einloggen
</button>

EDIT: Try this as per your request in comments:

<?php
    if (isset($_POST['btnLogin']))
    {
        sleep(3);
        redirect('Location:next page.php');     
    }
?>
James
  • 4,644
  • 5
  • 37
  • 48
  • Yes thanks also without results. if i just use sleep without anything the page delay on visit – anit234 Aug 07 '18 at 22:46
  • The page delays on visit? What do you want it to do? `sleep()` will literally halt the script entirely, nothing will happen for those 3 (or whatever) seconds. – James Aug 07 '18 at 22:55
  • I want sleep if first after i click my submit button and then redirect – anit234 Aug 07 '18 at 22:57
  • I've updated my answer. If that doesn't work you'll have to explain in detail what happens. – James Aug 07 '18 at 23:01
  • The best i dont know what happens.. Nothing really nothing makes an differant, i think the" if (isset($_POST['btnLogin']))" is the Problem? Can i write it on other way? – anit234 Aug 07 '18 at 23:06
  • At this stage I don't know what you need, or what happens in your code. The code I have given you works and is tested. If you are having issues still then you need to explain clearly what the issue is. Also consider that you might be under confusion as to what to expect from the code. The only time anything will happen is when you click the button. You need to have a form for this to work, but given you have a closing form tag I presumed you had this already. If that's not the case then we need more code. Basically, either way we need more info to be able to help you :) – James Aug 07 '18 at 23:09
  • Thanks for your help, maybe the Problem is that i run the script on Localhost with wamp server? – anit234 Aug 07 '18 at 23:11
  • @anit234 No thats not the issue – RiggsFolly Aug 08 '18 at 00:01