【问题标题】:Require_once is not executed on ajax callajax调用时不执行Require_once
【发布时间】:2012-10-24 03:32:51
【问题描述】:

这是我的“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);
        }

【问题讨论】:

  • 呃,AJAX 调用在哪里?我只看到一个 PHP 包含。
  • 我试过 require_once("$filename");但同样的事情发生
  • 我认为这不是一个好主意,但您也尝试过include("$filename"); 吗?如果你这样做了,结果如何?
  • Ignacio,我试过了,但没有解决!问题是当它通过'blog.php'包含时它可以工作,但是当通过ajax调用'blog_list.php'时,php不会执行'require_once'
  • 为什么不用require或include??

标签: php ajax require-once


【解决方案1】:

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

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-调用在pages-文件夹中找不到名为phpScripts 的目录,因此required_once 调用永远不会被执行。

【讨论】:

  • 谢谢罗比!这似乎是问题所在!
【解决方案2】:
// 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);

你可以得到所有的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2019-03-12
    相关资源
    最近更新 更多