Javascript 通过AJAX将PHP变量传递给另一个DIV中的PHP变量

Javascript 通过AJAX将PHP变量传递给另一个DIV中的PHP变量,javascript,php,jquery,python,ajax,Javascript,Php,Jquery,Python,Ajax,我已经为此工作了一整天,但我想我在学习AJAX的过程中对各种可用的方法感到困惑。我希望我的网站显示Python脚本的结果。我能做到 问题是脚本的结果会随机更改(这是我车库门的状态),如果车库门的状态更改,我的站点会变得笨重。通常,用户必须不断重新加载页面才能获得当前状态。我试图让显示状态的DIV每5秒更新一次,从而显示新的状态 Python脚本运行大约需要4秒钟,因此我希望继续将其作为函数调用,并将其作为DIV传递到我的站点上,我希望在那里显示结果 如果可能的话,一个PHP文件(index.PH

我已经为此工作了一整天,但我想我在学习AJAX的过程中对各种可用的方法感到困惑。我希望我的网站显示Python脚本的结果。我能做到

问题是脚本的结果会随机更改(这是我车库门的状态),如果车库门的状态更改,我的站点会变得笨重。通常,用户必须不断重新加载页面才能获得当前状态。我试图让显示状态的DIV每5秒更新一次,从而显示新的状态

Python脚本运行大约需要4秒钟,因此我希望继续将其作为函数调用,并将其作为DIV传递到我的站点上,我希望在那里显示结果

如果可能的话,一个PHP文件(index.PHP)。这是我想做的事情的框架。我的get_status函数可以工作,但我对它的其余部分一无所知

多谢各位

编辑:代码更新,注释者发现了一些小的调整

<html>
<body>

<?php
    function get_status(){
        $status = shell_exec('python /path/to/garage_door/myq-garage.py status');  //Equals either 'Open' or 'Closed'
        echo $status;
    }
?>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
    var java_status = <?php echo json_encode(get_status) ?>;

    // I have no idea how to pass a variable from PHP thru javascript to PHP on the 
    // same page.  Trying to pass on results of get_status function every 5 seconds.

    setInterval(function(){

        $.ajax({
            url: "index.php"
            type: "POST"
            dataType: "json"
            data: ({status: java_status}),
            success: function(data){
                $("#status_div").html(data);
            }
        })

    }, 5000);
</script>

<div id="status_div">
    <?php
        $new_status = json_decode(data);
    ?>        

    // I have no idea how to get that status variable here.
    The Garage Door Status is:  <?php 
        echo $new_status;
    ?>

</div>
</body>
</html>

var java_status=;
//我不知道如何通过javascript将变量从PHP传递到服务器上的PHP
//同一页。尝试每5秒传递get_status函数的结果。
setInterval(函数(){
$.ajax({
url:“index.php”
类型:“职位”
数据类型:“json”
数据:({status:java_status}),
成功:功能(数据){
$(“#status_div”).html(数据);
}
})
}, 5000);
//我不知道如何在这里获取状态变量。
车库门状态为:

创建一个页面并将其命名为status.php

status.php包含以下代码:

    $status = shell_exec('python /path/to/garage_door/myq-garage.py status');        
    echo $status;
制作另一个页面index.php并包含-

<div id="status_div">

</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>


   <script type="text/javascript">

    setInterval(function(){

        $.ajax({
            url: "status.php",

            dataType: "html",

            success: function(data){
                $("#status_div").html(data);
            }
        })

    }, 5000);
</script>

setInterval(函数(){
$.ajax({
url:“status.php”,
数据类型:“html”,
成功:功能(数据){
$(“#status_div”).html(数据);
}
})
}, 5000);

希望这将帮助您正确地执行此操作,您必须具有有效的HTML,并且不需要向PHP脚本发送任何参数。此外,您需要将PHP与其他代码分开,否则您将在AJAX响应中返回所有标记:

PHP脚本-PHP_python.PHP


HTML Page-index.php(注意使用文档就绪处理程序,因为脚本位于页面顶部)

您还需要分离
标记,使用一个来加载jQuery库,另一个来描述JavaScript函数

<html>
    <head>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(function(){ // you need a document ready handler if you put the script at the top of the page
            setInterval(function(){
                $.ajax({
                    url: "php_python.php",
                    type: "POST",
                    dataType: "text",
                    success: function(data){
                        $("#status_div").html('The Garage Door Status is: ' + data);
                    }
                })

            }, 5000);
        });
        </script>
    </head>
    <body>   
        <div id="status_div"></div>
    </body>
</html>

$(function(){//如果将脚本放在页面顶部,则需要一个文档就绪处理程序
setInterval(函数(){
$.ajax({
url:“php_python.php”,
类型:“POST”,
数据类型:“文本”,
成功:功能(数据){
$(“#status_div”).html('车库门状态为:'+数据);
}
})
}, 5000);
});

如果您只是在学习jQuery的AJAX。

如果您使用AJAX,您的生活会变得非常轻松:

函数_status.php:

<?php
function get_status(){
        $status = shell_exec('python /path/to/garage_door/myq-garage.py status');     //Equals either 'Open' or 'Closed'
        return $status; //avoid echo in such functions, try to not produce side effects
    }

#status_div
中根本不需要任何代码。您应该在success函数中解析
数据
,并在
#status_div
中放置必要的部分。Python代码是否返回JSON?如果这是HTML,则无效。
JSON_decode(data)
仅此一项就应该引发未定义的常量错误。您不能像这样将JS中的
数据
用于PHP函数。谢谢。我得试试。我得开车去某个地方,今晚晚些时候再试试,然后回来报到。这让我学到了一些我自己永远也想不到的东西。如果我能正确理解你想要的东西,它应该会起作用。。。thanksI刚刚尝试过,但是status.php和index.php都得到了一个空白页面。在index.php上,我在您的第三行发现了一个问题。我已将其更改为:在status.php中,您应该在php标记中添加这些代码,并且必须运行index.php才能查看状态。是的,这是一个语法错误。运行status.php时会得到什么结果??如果为空,那么您的shell_exec()就不起作用了,因为AJAX调用是异步的,无论脚本回显值需要多长时间都不重要,否?为真-但这对AJAX调用有何影响?如果python脚本需要4秒,则最初加载页面需要4秒,每个更新请求也需要4秒。异步性意味着其他javascript代码不必等待请求完成,但请求本身不会加速。t=0:get请求t=4:page加载,updateStatus调用1。时间,开始的5秒间隔t=8:1的响应。updateStatus调用到达t=9:间隔从2开始。updateStatus t=13的调用:2的响应。调用updateStatus t=14:间隔从3开始。调用updateStatus…如果PHP脚本是独立的,则立即加载页面,然后发出AJAX请求。4秒钟后,您应该会收到响应。是的,但在前4秒钟,您会看到一个没有状态的页面。那是你的选择。无状态的快速加载页面,或状态为的慢速加载页面。如果请求速度更快,我会将其分离。谢谢!经过一些修改,我可以让这个工作。在php_python.php中,由于没有调用函数,所以没有运行任何东西,所以我只是注释掉了函数行,同时保留了$status变量的创建和回显。谢谢你解释你的一些理由。这对我很有帮助,而且有些重要的事情我还没有意识到。剥猫皮的方法总是不止一种。您可以做的另一件事是添加一行,
get_status()操作。真的没有什么特别之处
<?php
function get_status(){
        $status = shell_exec('python /path/to/garage_door/myq-garage.py status');     //Equals either 'Open' or 'Closed'
        return $status; //avoid echo in such functions, try to not produce side effects
    }
<?php
require_once("function_status.php");
echo get_status();
<?php
require_once("function_status.php");
?>
<html>
<body>
<div id="status">
<?php echo get_status(); ?>
</div>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
<script type="text/javascript">

( //run function immediatly
  function($){ //in this anonymous function, $ is always jQuery
     function updateStatus(){
         $.ajax({
            url: "ajax_server.php"
            type: "POST"
            dataType: "json"
            data: ({status: 1}),
            success: function(data){
                $("#status").html(data);
            }
        });
     }

     //first call it onload
     $(function(e){
        updateStatus();
     }

     //and then every 5 seconds
     setInterval(updateStatus, 5000);
     );
   }

)//run function immediatly
(jQuery); //pass parameter jQuery to immediate execution of anonymous function



</script>
</body>
</html>
if(isset($_POST['status'])){
echo get_status();
}else{
//output the html around the whole thing
}