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

2024-05-13 20:54:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

如果可能,一个PHP文件(索引.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>

Tags: toechodiv脚本jsondataget状态
3条回答

要正确地做到这一点,您必须有有效的HTML,并且不需要向PHP脚本发送任何参数。此外,您需要将PHP与其余代码分开,否则您将在AJAX响应中获取所有标记:

PHP脚本-PHP_python.php文件

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

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

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

^{pr2}$

如果您只是学习jQuery的AJAXhere are some basic tips for setting it up and trouble-shooting problems。在

创建一个页面并命名它状态.php在

在状态.php包括以下代码:

    $status = shell_exec('python /path/to/garage_door/myq-garage.py status');        
    echo $status;

制作另一页索引.php包括-

^{pr2}$

希望这对你有帮助

如果您使用的是ajax,您可以让您的生活非常轻松:

功能_状态.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
    }

阿贾克斯_服务器.php公司名称:

^{pr2}$

在索引.php公司名称:

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

这不是一种非常干净的方法,我只使用了<?php echo get_status(); ?>,因为您的python脚本需要4秒,所以在前4秒中没有状态。 否则你可以改成索引.html如果你想用ajax填充html的话,还有一个很好地分离的html和php。 如果你真的想把它放到一个文件中,你需要一个

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

相关问题 更多 >