PHP中找不到类错误

I am a rookie PHP and MongoDB developer.

I have created a PHP web project with an HTML page that contains an 'Add' button. The name of the page is awards.html. The awards.html file contains its counterpart JavaScript file, awards.js. A code is executed in this js file when the Add button is clicked. This code sends an AJAX call to a PHP class elsewhere in the project named, example.php which contains code to execute a function called, clickFunction() in an Awards.php file, which returns a JSON array to the awards.html page.

The source code of my files is given as follows:

Awards.html

<div class = "divbottom">
    <div id="divAddAward">
        <button class="btn" onclick="onrequest();">Add</button>
    </div>
</div>

awards.js

function onrequest() {
    $("#divAddAward").load('branding/dataaccess/example.php'); //The full path of the example.php file in the web root
    alert('Test');
    $.post(
            'branding/dataaccess/example.php'
            ).success(function(resp) {
        json = $.parseJSON(resp);
        alert(json);
    });
}

example.php

<?php

foreach (glob("App/branding/data/*.php") as $filename) {
    include $filename;
}

$class = new Awards();
$method =  $class->clickFunction();
echo json_encode($method);

Awards.php

<?php

class Awards extends Mongo_db {

    //put your code here

    public function __construct() {
        parent::__construct();
    }

    public function clickFunction() {
        $array = array(
            'status' => '1'
        );
        return $array;
    }

}

The problem here is that the program is throwing me an error in example.php, Class 'Awards' not found, despite adding the for loop to include all the files. The Awards.php file is located in App/branding/data/ path and example.php is located in App/branding/dataaccess/ path.

Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.

Update example.php to

<?php

foreach (glob('../data/*.{php}', GLOB_BRACE) as $filename) {
    //Echo out each included file to make sure it's included
    echo $filename . '<br />'; 
    include $filename;
}

$class = new Awards();
$method =  $class->clickFunction();
echo json_encode($method);

My guess: as you said, example.php is located in App/branding/dataaccess/, so when you do your loop you're actually searching for files in App/branding/dataccess/App/branding/data which doesn't exist so nothing is included.

Try this:

foreach (glob("../data/*.php") as $filename) {
    include $filename;
}

hmm.. try to use spl_autoload_register(); to include or require all files at once on example.php

spl_autoload_register(function($class){
    require_once '../data/' . $class . '.php';
});