0

Ok so, this is part of a fancy "smart home" webpage im making, and I cant seem to get this code to run in the correct order, no matter what I do, even though I put a sleep() before the script execution, it refuses to show the success message before it runs the script that reboots the router (which is why I need it to show BEFORE, since this page isint available again until the router fully reboots). Hopefully someone can help me with this!

(At this point, #cover-spin is set to 0 and showing, hence why I set cover-spin.show to 1, to hide it before the message appears)

(yes, #cover-spin is a css class, no idea why I had to put it in javascript tags, but it does work if I put it in another PHP script that always runs on the page)

Heres the code:

if (0 == $status) {
    echo '<script type="text/javascript">';
    echo '$("#cover-spin").show(1)';
    echo '</script>';
    echo '<script type="text/javascript">';
    echo 'setTimeout(function () { swal("Success","Command was successfully sent","success");';
    echo '}, 0);</script>';
    sleep(3);
    exec("/var/www/html/router.sh");
} else {
    header("Location: ?error=true");
}

edit: something like this?

echo '<script type="text/javascript">';
    echo 'objXMLHttpRequest.open("GET", "router.sh");';
    echo 'objXMLHttpRequest.send();';
    echo '</script>';
3
  • 6
    PHP runs on the server (therefore it runs first) Then when it finishes the output is shipped to the browser WHERE Javascript runs. Commented Jan 29, 2020 at 21:44
  • 4
    If you want to run PHP code after JS code, you need AJAX / fetch and make a second request to the server. Commented Jan 29, 2020 at 21:45
  • 1
    or fetch('router'sh').then(r=>{}) etc Commented Jan 29, 2020 at 21:55

1 Answer 1

1

PHP functions on the server-end, constructing an HTML document based on your instructions. Once this is generated, it delivers the finished product, clean of any PHP at all, to your web browser. Javascript and anything else that was constructed by the PHP will not run until it is executed by the web browser. To accomplish what you want, use an ajax call to execute any PHP you want executed after page load.

P.S. Even if the php and javascript could run at the same time, the PHP is literally creating the javascript, so it would by definition HAVE to run first for the javascript to even exist in the first place, no?

Here's some info on AJAX.

Sign up to request clarification or add additional context in comments.

3 Comments

Good answer, but please do not send people to w3schools
@ChrisG Im gonna keep doing it, they've never led me astray albeit sometimes outdated. They've improved in quality tremendously since 5 years ago when that post was made
We keep getting newbies talking about "JSON Objects". Guess which is the only site spreading that nonsense? When there are better resources out there (like MDN), why keep sending people to worse sites?

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.