Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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 php页面每60秒仅刷新div部分_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript php页面每60秒仅刷新div部分

Javascript php页面每60秒仅刷新div部分,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有一个php网页,它打印html命令并执行其他操作。我有一个部门: <div id="monitor" class="monitoring"><?php include "php/monitoring.php"?></div> index.php脚本中的此脚本应每60秒调用一次。所以我发现AJAX和JavaScript在这方面很有用。因此,我使用以下脚本: <script type="text/javascript"> window.set

我有一个php网页,它打印html命令并执行其他操作。我有一个部门:

<div id="monitor" class="monitoring"><?php include "php/monitoring.php"?></div>

index.php脚本中的此脚本应每60秒调用一次。所以我发现AJAX和JavaScript在这方面很有用。因此,我使用以下脚本:

<script type="text/javascript">
window.setInterval(function () {executemonitor()}, 60000);

function executemonitor() {
    $("#monitor").load("php/monitoring.php");
}

setInterval(函数(){executemonitor()},60000);
函数executemonitor(){
$(“#monitor”).load(“php/monitoring.php”);
}


脚本工作正常,但问题是,monitoring.php脚本的第二次运行失败。脚本无法使用$\u会话变量。我得到错误:注意:未定义的变量:\在第168行C:\xampp\htdocs\mi hand\php\monitoring.php中的会话。

如果我使用html刷新命令,它会工作

<meta http-equiv="refresh" content="n" />


但随后整个页面被刷新。我想避免这种情况。我只想通过执行php脚本来刷新div部分。我该怎么做?monitoring.php文件是一个单独的HTML页面。

如果您直接访问
“php/monitoring.php”
,则需要在该脚本的开头使用
session\u start()
,而不仅仅是在
index.php

中,只有在调用session\u start()之后,\u session变量才在php中可用

您的客户端调用2个不同的脚本:

  • index.php,它呈现div并包括monitoring.php
  • 显示div内容的monitoring.php
index.php中有一个session_start(),但monitoring.php中没有,因此当客户端直接调用monitoring.php时,这无法工作

您可以创建一个session.php文件来进行会话初始化(比如调用session_start()),然后在index.php和monitoring.php中使用它

include_once('session.php');
如果您的php>5.4,还可以在monitoring.php中添加以下代码

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

来源:

您是否在monitoring.php顶部给出了
session\u start()
“脚本工作正常,但问题是,monitoring.php脚本的第二次运行失败。脚本无法使用$\u会话变量。我得到一个错误:注意:未定义的变量:_SESSION位于C:\xampp\htdocs\mi hand\php\monitoring.php的第168行。“您至少应该向我们展示php文件中的代码。在包含div部分之前,我在index.php中调用SESSION_start()。SESSION_start()应该在每个页面中使用SESSION_start(),使用SESSION id您单独放置SESSION_start();在monitoring.php中,因为如果您在index.php中启动会话,则可以刷新整个页面并使用index.php命令,但当您单独调用页面monitoring.php时,必须在monitoring.php页面顶部提供会话_start()