I'm still new to web development and I'm having some problems figuring this one out. I have the code below. A button triggers a .sh script that uploads some files while filecount.php checks the amount of files left in a directory and echo's the results.
My idea was that when you press the button, filecount.php refreshes and gives the user an idea of the amount of files left.
But when I look at my browser's dev console I see that as soon as I press the button (SendBtn) that executes the .sh, the returned filecount.php is 0 bytes (don't see a 200 get response either). Only when the post if finished filecount.php is returned correctly but at that point there are always no files left.
How can I make my page show and refresh the output of filecount.php during post?
page.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>Test</h1>
<form method="post">
<div style="display:block;">
<button id="SendBtn" name="SendBtn">Start</button>
</div>
</form>
<?php
if (isset($_POST['SendBtn'])) {
$output = shell_exec("/usr/local/bin/www/test.sh");
}
?>
<button onclick="stopRefresh()">Stop</button>
<div id="container">Starting...</div>
<script>
function refreshData() {
$('#container').load('filecount.php');
}
function stopRefresh() {
window.clearInterval(myTimer);
}
var myTimer = window.setInterval(refreshData, 1000);
</script>
</body>
</html>