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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?