在ajax调用上不执行Require_once

时间:2012-11-03 19:38:27

标签: php ajax require-once

这是我的'index.php'文件(部分内容):

<?php

// Require every .php file inside "phpClasses" folder
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();

// 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();
}
?>
<head>
...
</head>
<body>        
    <div id="cur_content" class="temp_content" data-tempPos="0">
    <?php 
        include 'pages/home.php'; 
    ?>
    </div>  <!-- #cur_content -->
</body>

在#cur_content内部我通过ajax调用注入'blog.php':

'blog.php的':

<div id="blog_main_content" class="temp_content">
    <?php
        include "blog_list.php";
    ?>
</div>

..在里面,我包含'blog_list.php':     

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();

$dbQuery = new databaseQuery();     

// Get the language from loader.php
$cur_lang = "'".$_SESSION['language']."'";

$dbQuery->getArticleList($cur_lang);

 ?>

在#blog_main_content div中,我通过ajax调用注入'articleLoader.php'并且工作正常。 'blog_list.php'第一次显示得很好。 当用户通过ajax调用返回'blog_list.php'时,我收到以下错误:

致命错误:第8行的C:\ wamp \ www \ kapantzakis_2.14 \ pages \ blog_list.php中找不到“数据库”类

我认为当ajax调用此文件时,php不会在'blg_list.php'中执行require_once。

我不知道我是否能解释清楚。

感谢您的帮助!

修改#1

Ajax电话:

// Perform the ajax call
        function getAjaxPage(method, content, currentOffset) {

            var temp_content = $('.temp_content');                      
                var temp_content_last = temp_content.filter(':last');

            var blog_main_content = $('#blog_main_content');                    
                var blog_main_content_first = blog_main_content.filter(':first');

                // Insert the html data in to the first or last div depending on the movement of the page
                if (method == 'next') {
                    var insert_div = temp_content_last;
                } else if (method == 'prev') {
                    var insert_div = blog_main_content_first;
                }

                // Get article or the article list
                if (content == 'article') {
                    var page = 'articleLoader.php';
                } else if (content == 'article_list') {
                    var page = 'pages/blog_list.php';                       
                }
                /*
                var lang = getCurLang();
                var data = 'lang=' + lang + '&art_id=' + art_id;*/

                var tags_wrapper = $('#tags_wrapper');

            $.ajax({
                url: page,  
                type: "GET",    
                /*data: data,*/
                cache: false,
                success: function (html) {                          
                    insert_div.html(html)
                        .queue(function() {
                            var return_to_list = $('#return_to_list');                              
                            return_to_list.attr('data-offsetTop', currentOffset);   
                            returnTopOffset();
                            if (content == 'article') {
                                tags_wrapper.fadeIn(800);
                            } else if (content == 'article_list') {
                                tags_wrapper.hide();
                            }                               
                            $(this).dequeue();
                        });
                }       
            });
            return $(this);
        }

2 个答案:

答案 0 :(得分:2)

我假设您的文件夹结构如下所示:

kapantzakis_2.14/
  articleLoader.php
  blog.php
  blog_list.php
  index.php
  pages/
    home.php
  phpScripts/
    Database.php

您的AJAX-Call直接请求pages/blog_list.php,但在您的问题文本中,似乎blog_list.php位于根目录(kapantzakis_2.14)中。

您收到的错误([...] 'Database' not found in C:\wamp\www\kapantzakis_2.14\pages\blog_list.php [...])显示blog_list.php位于pages - 文件夹中。您的glob - 调用无法在phpScripts - 文件夹中找到名为pages的目录,因此required_once调用永远不会被执行。

答案 1 :(得分:0)

// add this line on the top of your script
ob_start();


// add this line on the bottom of your script
$errors = ob_get_contents();
ob_end_clean();
$fopen = fopen('errors.txt', 'w+');
fwrite($fopen, $errors);
fclose($fopen);

你可以得到所有错误