简体   繁体   English

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

[英]CSS and PHP to run exec and return to original page

Kind of a newbie to web coding, and I am trying to achieve something simple: Show a page with a big button image on it, and when clicked, it will execute a simple command line on my Raspberry Pi. 有点像Web编码的新手,我正在尝试实现一些简单的操作:在页面上显示带有大按钮图像的页面,当单击该页面时,它将在我的Raspberry Pi上执行简单的命令行。 After the exec is done, I would like to return to the same page ready for another press of the button. 执行完毕后,我想返回同一页面,准备再次按下该按钮。

So, I am using the following: 因此,我正在使用以下内容:

<?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>

This works, however, after the exec finishes, the browser is not redirected back to the original page, or it does, but the url is: 这可以执行,但是,在exec完成后,浏览器不会重定向回原始页面,或者可以重定向到原始页面,但是url为:

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

Which have the GET paramerts. 其中具有GET参数。 Therefore, I cannot click the button again, until I either hit the back button or refresh button on the browser. 因此,在点击浏览器上的“后退”按钮或“刷新”按钮之前,我无法再次单击该按钮。

Any thoughts...? 有什么想法吗...?

a more elegant solution could be to use ajax, i see that you already have jquery so you could include a script with this code 一个更优雅的解决方案可能是使用ajax,我看到您已经有了jquery,因此可以在此代码中包含一个脚本

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

this will call the site without caring about the response and it will execute your script, i would suggest using a <button></button> tag instead of <a></a> to avoid redirection 这将调用该站点而不关心响应,它将执行您的脚本,我建议使用<button></button>标记代替<a></a>以避免重定向

If you would like to improve your code further i would separate the script from the html and maybe call it with POST instead of GET 如果您想进一步改善代码,我会将脚本与html分开,并可能使用POST而不是GET进行调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM