doz59484 2013-07-19 12:07
浏览 42
已采纳

PHP包括函数和AJAX div加载

I have a php generated web page, which has several divs that are initially created using php includes.

<div id ="siteLeft" class ="site">
    <div id="divGauges"><?php include('gauges.php');?></div>
    <div id="divHistory"><?php include('history.php');?></div>
</div>
<div id="siteRight" class ="site">
    <div id="divLymGauges"><?php include('gaugesLym.php');?></div>
    <div id="divLymHistory"><?php include('historyLym.php');?></div>
</div>

These divs are then reloaded every 10 seconds, using an AJAX call, which is in a seperate scripts.js file:

function updateHistory(){
    $('#divHistory').load('history.php'+"?ms=" + new Date().getTime());
    $('#divLymHistory').load('historyLym.php'+"?ms=" + new Date().getTime());
}
setInterval( "updateHistory()", 10000 );

Within the history.php and historyLym.php, I have a function call, and the function is in another file called functions.php.

If I do:

include ('functions/functions.php');

within the index.php page, then the div's display nicely on initial load, but I get

Fatal error: Call to undefined function 

displaying on the webpage after the AJAX call refreshes the div with the history.php and historyLym.php (obviously, as the include for the function file isn't in the div, which is the only bit that gets called).

So I tried putting the functions.php include into the divs that are loaded. If I do that, then on initial page load I get:

Fatal error: Cannot redeclare getForce()

which also makes sense, as on initial load the two divs both contain the include for functions.php.

Putting the include in only one of the divs results in the page loading fine (as it should do), but on the AJAX refresh of the divs, I get this error:

Fatal error: Call to undefined function getForce()

which again makes sense as the two divs are being refreshed independently by AJAX, and so only the div with the include for functions.php will be able to use the function.

So my questions is:

Where on earth can I put the include for functions.php so that the divs can use the functions.php functions on initial page load AND on individual seperate AJAX refreshes??

  • 写回答

3条回答 默认 最新

  • dscrb00804 2013-07-19 12:19
    关注

    First include the function file in your index.php,
    Then add this at the top of your include files (ie: gauges.php, history.php, etc..)

    <?php
      if ( !function_exists( "getForce" ) ) {
        // include the function file
      }
    ?>
    

    You can replace getForce with any other function names which is defined in your function file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?