1

I want to add a loading spinner while my remote script is running and my frontend page is in "waiting for ..." state. I only have 1 php page and one CSS . ( for the moment still in tests so I have not split script page and html page yet).

I have the following css for the loader :

Style.css edited to replace the "." with "#"

#loader {
    border: 16px solid #f3f3f3; /* Light grey */
    border-top: 16px solid #3498db; /* Blue */
    border-radius: 50%;
    width: 120px;
    height: 120px;
    animation: spin 2s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

My page looks like this ( after cleaning it ) : EDIT with webbm comments

<!DOCTYPE html>
<html>
<head>
  <title> BETA APP HOME PAGE </title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  <link rel="stylesheet" href="CSS/style.css">
</head>
<body>

<div id="loader"></div>

<?php
if (isset($_POST['STARTVISUREC']))
{

   echo '<script>console.log("End loader spinner")</script>';
// making appearing the spinner before the call to remote server. 
    echo '<script language="javascript">';
    echo 'document.getElementById("loader").style.display="inline"';
    echo '</script>';

// calling remote APP in BE server. 
$remotstarter = file_get_contents('http://192.168.56.154/WEBAPP/wa_start.php');

   echo '<script>console.log("End loader spinner")</script>';
// making disappearing the spinner after the call to remote is finished. 
    echo '<script language="javascript">';
    echo 'document.getElementById("loader").style.display="hidden"';
    echo '</script>';

}
if (isset($_POST['STARTFACEREC']))
{
// calling local script for other usage. 
shell_exec("sudo python  AppPy/cgi-bin/test.py");
echo("Off");
}
?>

<form method="post">
<button name="STARTVISUREC">START VISU REC</button>&nbsp;
<button name="STARTFACEREC">START FACE REC</button><br><br>
</form> 
<script>
</script>
</div>
</div>
<div style="margin-left:15%;padding:1px 16px;height:10px;">
</div>
</body>
</html>

still not OK The loading spinner is not appearing

MouIdri
  • 1,300
  • 1
  • 18
  • 37

1 Answers1

1

For your case specifically, you could echo a script tag.

<?php
 echo '<script language="javascript">';
 echo 'document.getElementById("loader").style.display="inline"';
 echo '</script>';
?>

Note that I switched around the usage of " and '. Check out the echo documentation for why this is useful https://secure.php.net/manual/en/function.echo.php

I recommend you look into ajax queries which can allow you to perform the javascript actions inside of javascript based on the server response.

Here's a basic example: https://www.w3schools.com/php/php_ajax_php.asp

webbm
  • 634
  • 8
  • 18
  • I changed it and I also replaced :
    with
    but still not OK
    – MouIdri Apr 26 '17 at 11:57
  • @MouIdri can you post an update to your original code? – webbm Apr 26 '17 at 11:59
  • @MouIdri I updated my code a little to make it nicer to change and removed an erroneous semi-colon so update yours to reflect my latest one. – webbm Apr 26 '17 at 12:32
  • @MouIdri You may also be getting a response so quickly that the loader div doesn't have time to display before it is hidden again, I suggest adding some `console.log('Displaying');` to test whether or not the script is firing. – webbm Apr 26 '17 at 12:36
  • webbm : It is working actually my error was due to a finger check, in the CSS, I wrote "." instead of "#" before loader. I edited it. However, I have a strange behavior now. Meaning the spinner is runing before I am clicking on the button.. – MouIdri Apr 26 '17 at 12:55
  • @MouIdri that's because the php is running as soon as the page is loaded, which is by design. Check out this question: https://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button it basically comes back to my suggestion about using ajax. – webbm Apr 26 '17 at 13:01
  • So this is not possible to start the loading after clicking the button with this current approach ? – MouIdri Apr 26 '17 at 13:22
  • You need a way to call the php when you click the button. That's what ajax is for. – webbm Apr 26 '17 at 13:52