ajax调用后PHP“重新要求”文件

时间:2012-10-25 19:56:13

标签: php ajax

在我的index.php文件中,我首先需要一些文件并使用php连接到数据库..

<?php

// Require every .php file inside "phpClasses" folder
foreach (glob("phpScripts/*.php") as $filename) {
require $filename;
}

// Create the $db object
$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 

// Connect to the database
$db->connect();

// Instantiate the "language" and "databaseQuery" classes
$lang = new language();     
$dbQuery = new databaseQuery();     

// Detect if the laguage has changed from the user and apply the new "current language"
if(isset($_GET["change_lang"])) {
    $change_lang = $_GET["change_lang"];
    $cur_lang = $change_lang;
} else {
    $cur_lang = $lang->getCurLang();
}
?>

在同一个文件中,我包含'home.php'文件来显示数据库内容..

<div id="cur_content" class="temp_content" data-tempPos="0">
        <?php 
            include 'pages/home.php'; 
        ?>
    </div>  <!-- #cur_content -->

在'home.php'文件中,我重新创建$ dbQuery类并调用getText()函数,如下所示:

<?php

$dbQuery = new databaseQuery();         

?>

<div id="content_home" class="cur_content_inner"> 

  <?php
$text_pos = 'home_inner_1';
    $dbQuery->getText($text_pos, $cur_lang);
  ?>

</div>

'index.php'仍然没有刷新,用户通过ajax调用导航。新内容加载到#cur_content div中,删除前一个。

在第一次加载页面时一切正常,但是当再次请求'home.php'时(没有index.php刷新),我得到php错误,因为最初的'require'没有发生。

在ajax调用之后,如何再次要求这些文件?

抱歉,我是新来的! ;) 感谢每一个帮助!

问题解决了 - &gt; http://codeigniter.com/forums/viewthread/59450/

我用过这个:

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest") {
    foreach (glob("phpScripts/*.php") as $filename) {
        require_once $filename;
    }

    // Create the $db object
    $db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 

    // Connect to the database
    $db->connect();
}

1 个答案:

答案 0 :(得分:0)

你需要做的是这样的事情:

将此代码段放在一个单独的文件中,例如includes.php

// Require every .php file inside "phpClasses" folder
foreach (glob("phpScripts/*.php") as $filename) {
  require $filename;
}

现在将其从index.php中移除,只需将includes.php添加到所有导航页面中,包括home.php

相关问题