Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 运行通过AJAX传递的Python脚本的PHP命令按钮不起作用_Javascript_Php_Jquery_Python_Ajax - Fatal编程技术网

Javascript 运行通过AJAX传递的Python脚本的PHP命令按钮不起作用

Javascript 运行通过AJAX传递的Python脚本的PHP命令按钮不起作用,javascript,php,jquery,python,ajax,Javascript,Php,Jquery,Python,Ajax,我仍然在努力学习AJAX和各种实现方法。我在我的网站上有一个DIV标记,我正在使用AJAX在5秒钟后通过将status.php文件重新加载到DIV标记中来动态更新它(定时更新是出于不相关的原因) 我遇到的问题是,当我直接浏览status.php时,命令按钮工作正常,但是当我通过index.php使用它们时,它们没有响应 我试图了解他们为什么会以这种方式失败,以及我能以一种“ELI5”的方式在工作中做些什么 命令按钮运行Python脚本,用于打开/关闭车库门。下次运行状态Python脚本(位于st

我仍然在努力学习AJAX和各种实现方法。我在我的网站上有一个DIV标记,我正在使用AJAX在5秒钟后通过将status.php文件重新加载到DIV标记中来动态更新它(定时更新是出于不相关的原因)

我遇到的问题是,当我直接浏览status.php时,命令按钮工作正常,但是当我通过index.php使用它们时,它们没有响应

我试图了解他们为什么会以这种方式失败,以及我能以一种“ELI5”的方式在工作中做些什么

命令按钮运行Python脚本,用于打开/关闭车库门。下次运行状态Python脚本(位于status.php顶部)时,它将具有门的新状态,并最终在AJAX更新页面的该部分时显示在DIV标记中

当我单击按钮时,index.php仍显示在其中,但DIV标记完全重新加载status.php。但是,当发生这种情况时,Python脚本似乎不会被触发

我怀疑命令按钮需要以更AJAX/javaey的方式运行,而不是通过PHP运行,但我甚至不知道从哪里开始

哦,还有一件事:我还有一个CGI脚本(e.h.),我也可以用它来开门/关门。但我不使用它来表示状态,因为它不像完整的Python脚本那样包含上次更改状态的时间/日期。这可能是一种更好地打开/关闭门的潜在方法

index.php:

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
        <script type="text/javascript">
        $(function(){ 
            setInterval(function(){
                $.ajax({
                    url: "status.php",
                    type: "POST",
                    dataType: "text",
                    success: function(data){
                        $("#status_div").html('The Garage Door Status is: ' + data);
                    }
                })

            }, 5000);
        });
        </script>
    </head>
    <body>
        Somethign on the web page.  Normally this would be the main website while the DIV tag waits to load.
        <div id="status_div"></div>
    </body>
</html>

$(函数(){
setInterval(函数(){
$.ajax({
url:“status.php”,
类型:“POST”,
数据类型:“文本”,
成功:功能(数据){
$(“#status_div”).html('车库门状态为:'+数据);
}
})
}, 5000);
});
网页上的一些东西。通常,当DIV标记等待加载时,这将是主网站。
status.php:

<?php

// Call Python script which gives me the status of my garage door in a text string.
$output = shell_exec('python /home/blah/garage_door/myq-garage.py status');

//I am NOT including lots of parsing of the $output.  Eventually I get the $output trimmed down to the to the $status PHP variable.
$status[1] = "Closed"   //values here can be either 'Closed' or 'Open'

?>

<!-- // Style info for table(s) -->
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:7px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:7px 7px;border-style:solid;border-width:1px;overflow:hidden;wor$
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-yw4l{vertical-align:center;text-align:left}
</style>

<!-- // HTML table to display garage door status info.  Display the image of the door and door status in left cell, time/date and options in right -->
<center>
<table class="tg">
<tr>
    <th class="tg-baqh">
        <div class="garage_status">
            <?php
                //Stuff unrelated to OP question here.  Dynamically displaying an image based on garage door status.
             ?>
        </div>
    </th>
    <th class="tg-yw4l">
        <?php
            // Command button to open door.
            if (isset($_POST['button_open'])){
                if ($status[1] = "Closed"){
                    shell_exec('python /home/blah/garage_door/myq-garage.py open "Garage Door"');
                }
            }

            // Command button to close door.
                if (isset($_POST['button_close'])){
                    if ($status[1] = "Open"){
                        shell_exec('python /home/blah/garage_door/myq-garage.py close "Garage Door"');
                        echo "<script>alert (\"cmd button works\")</script>";
                    }
                }

        ?>
        <form method="post">
            <p>
                <button name="button_open">Open Door</button>
                <button name="button_close">Close Door</button>
            </p>
        </form>
    </th>
</tr>
</table>
</center>
<script>
function open_door() {
    jQuery(function($) {
        $.ajax( {
            url : "http://example.com/cgi-bin/myq-cgi.py?user=somethibng@hotmail.com&pass=dfgsdfgsdfg&cmd=open",
            type : "GET",
            success : function(data) {
                alert ("Opening Door..."); //or use data string to show something else
                }
            });
        });
    }
</script>

<script>
function close_door() {
    jQuery(function($) {
        $.ajax( {
            url : "http://example.com/cgi-bin/myq-cgi.py?user=something@hotmail.com&pass=dfgdfgsdfg&cmd=close",
            type : "GET",
            success : function(data) {
                alert ("Closing Door..."); //or use data string to show something else
                }
            });
        });
    }
</script>


<form>
<input type="button" value="open" onClick="open_door()"
</form><form>
<input type="button" value="close" onClick="close_door()"
</form>

.tg{边框折叠:折叠;边框间距:0;}
.tg td{字体系列:Arial,无衬线;字体大小:14px;填充:7px 5px;边框样式:实心;边框宽度:1px;溢出:隐藏;分词:正常;}
.tg th{font系列:Arial,无衬线;字体大小:14px;字体重量:正常;填充:7px 7px;边框样式:实心;边框宽度:1px;溢出:隐藏;工作$
.tg.tg baqh{文本对齐:中心;垂直对齐:顶部}
.tg.tg-yw4l{垂直对齐:居中;文本对齐:左}

@hassan让我思考了一下,所以我在index.php上构建了我的命令按钮,这样它们就不在ajax响应中了

我让他们工作。车库门打开/关闭。index.php重新加载ajax的速度足够快,用户可以看到状态,例如从关闭-->打开-->打开-->关闭-->关闭的更改。更重要的是,页面不会像旧的php按钮那样进行整体重新加载

这次我用HTML/javascript而不是PHP来做按钮,也不是运行Python脚本,而是使用一个不同的基于Python的CGI脚本,正如我在OP中提到的

一旦它在index.php中工作了,我就把它复制到status.php中,当它被ajax加载时,它仍然可以正确地在里面工作,并且可以执行

以下是javascipt,它最初出现在index.html中,但最终转移到status.php:

<?php

// Call Python script which gives me the status of my garage door in a text string.
$output = shell_exec('python /home/blah/garage_door/myq-garage.py status');

//I am NOT including lots of parsing of the $output.  Eventually I get the $output trimmed down to the to the $status PHP variable.
$status[1] = "Closed"   //values here can be either 'Closed' or 'Open'

?>

<!-- // Style info for table(s) -->
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:7px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:7px 7px;border-style:solid;border-width:1px;overflow:hidden;wor$
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-yw4l{vertical-align:center;text-align:left}
</style>

<!-- // HTML table to display garage door status info.  Display the image of the door and door status in left cell, time/date and options in right -->
<center>
<table class="tg">
<tr>
    <th class="tg-baqh">
        <div class="garage_status">
            <?php
                //Stuff unrelated to OP question here.  Dynamically displaying an image based on garage door status.
             ?>
        </div>
    </th>
    <th class="tg-yw4l">
        <?php
            // Command button to open door.
            if (isset($_POST['button_open'])){
                if ($status[1] = "Closed"){
                    shell_exec('python /home/blah/garage_door/myq-garage.py open "Garage Door"');
                }
            }

            // Command button to close door.
                if (isset($_POST['button_close'])){
                    if ($status[1] = "Open"){
                        shell_exec('python /home/blah/garage_door/myq-garage.py close "Garage Door"');
                        echo "<script>alert (\"cmd button works\")</script>";
                    }
                }

        ?>
        <form method="post">
            <p>
                <button name="button_open">Open Door</button>
                <button name="button_close">Close Door</button>
            </p>
        </form>
    </th>
</tr>
</table>
</center>
<script>
function open_door() {
    jQuery(function($) {
        $.ajax( {
            url : "http://example.com/cgi-bin/myq-cgi.py?user=somethibng@hotmail.com&pass=dfgsdfgsdfg&cmd=open",
            type : "GET",
            success : function(data) {
                alert ("Opening Door..."); //or use data string to show something else
                }
            });
        });
    }
</script>

<script>
function close_door() {
    jQuery(function($) {
        $.ajax( {
            url : "http://example.com/cgi-bin/myq-cgi.py?user=something@hotmail.com&pass=dfgdfgsdfg&cmd=close",
            type : "GET",
            success : function(data) {
                alert ("Closing Door..."); //or use data string to show something else
                }
            });
        });
    }
</script>


<form>
<input type="button" value="open" onClick="open_door()"
</form><form>
<input type="button" value="close" onClick="close_door()"
</form>

功能打开门(){
jQuery(函数($){
$.ajax({
url:“http://example.com/cgi-bin/myq-cgi.py?user=somethibng@hotmail.com&pass=dfgsdfg&cmd=open“,
键入:“获取”,
成功:功能(数据){
警报(“开门…”);//或使用数据字符串显示其他内容
}
});
});
}
功能关闭门(){
jQuery(函数($){
$.ajax({
url:“http://example.com/cgi-bin/myq-cgi.py?user=something@hotmail.com&pass=dfgdfgsdfg&cmd=close“,
键入:“获取”,
成功:功能(数据){
警报(“关门…”);//或使用数据字符串显示其他内容
}
});
});
}

你不能在ajax响应中触发事件这很糟糕,但很有意义。看起来我需要创造性地在index.php上创建命令按钮,但要让它们看起来像status.php的一部分。即使我不能触发事件,有没有办法在ajax响应中的URL中触发cgi脚本?你可以n从堆栈中排除PHP-flask(python框架)是创建包含一些python代码的简单网页的简单方法。这只是我的观点,但我建议您排除可能被排除的相同技术。