CSS和PHP运行exec并返回原始页面


CSS and PHP to run exec and return to original page

有点像网络编码的新手,我正在尝试实现一些简单的事情:显示一个带有大按钮图像的页面,单击时,它将在我的树莓派上执行一个简单的命令行。执行完成后,我想返回同一页面,准备再次按下按钮。

因此,我正在使用以下内容:

<?php
if(isset($_GET['trigger']) && $_GET['trigger'] == 1)
{
    error_reporting(E_ALL);
    exec('<something>');
    usleep(1000000);
    exec('<something>');
    header("Location: push.php");
    exit;
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
    <div id="container">
        <a href='push.php?trigger=1' class="btn"></a>
    </div>
</body>
</html>

这有效,但是,在exec完成后,浏览器不会重定向回原始页面,或者确实如此,但网址是:

http://10.0.0.1/push.php?trigger=1

其中有GET参数。因此,我无法再次单击该按钮,直到我点击浏览器上的后退按钮或刷新按钮。

有什么想法吗?

一个

更优雅的解决方案可能是使用 ajax,我看到你已经有了 jquery,所以你可以包含带有此代码的脚本

var button = $('.btn')
$(document).on('click', '.btn', function(){
    button.css('display', 'none');
    $.get('push.php?trigger=1', function(){
        button.css('display', 'block');
    });
});

这将调用站点而不关心响应,它将执行您的脚本,我建议使用 <button></button> 标签而不是 <a></a> 以避免重定向

如果你想进一步改进你的代码,我会将脚本与html分开,也许用POST而不是GET调用它