Search code examples
phpajaxrequire-once

Require_once is not executed on ajax call


This is my 'index.php' file (part of it):

<?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>

Inside #cur_content I inject 'blog.php' via ajax call:

'blog.php':

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

..and inside it, I include '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);

 ?>

Inside #blog_main_content div, I inject 'articleLoader.php' via ajax call and works fine. 'blog_list.php' is displayed just fine for the first time. When user returns to 'blog_list.php' via ajax call, I get the following error:

Fatal error: Class 'Database' not found in C:\wamp\www\kapantzakis_2.14\pages\blog_list.php on line 8

I think that php does not execute the require_once in 'blg_list.php' when ajax calls this file.

I don't know if I explain well.

Thanks for any help!

edit#1

Ajax call:

// 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);
        }

Solution

  • I assume your folder structure looks like this:

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

    Your AJAX-Call requests pages/blog_list.php directly, but in your question-text it seems like blog_list.php is located in your root directory (kapantzakis_2.14).

    The error you get ([...]'Database' not found in C:\wamp\www\kapantzakis_2.14\pages\blog_list.php[...]) shows the blog_list.php is located in the pages-folder instead. Your glob-call cannot find a directory called phpScripts in the pages-Folder, so the required_once calls never get executed.