title: Performing CRUD Operations in MongoDB Using PHP Application
body: <p>I am a rookie PHP and MongoDB developer.</p>
  
  <p>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, connect() that is in an Awards.php file.</p>
  
  <p>The source-code of my files is given as follows:</p>
  
  <p><em>Awards.html</em></p>
  
  <pre><code>&lt;div class = "divbottom"&gt;
      &lt;div id="divAddAward"&gt;
          &lt;button class="btn" onclick="onrequest();"&gt;Add&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><em>awards.js</em></p>
  
  <pre><code>function onrequest() {
      $("#divAddAward").load('example.php');
      alert('Test');
      $.post(
              'example.php'
              ).success(function(resp) {
          json = $.parseJSON(resp);
          alert(json);
      });
  }
  </code></pre>
  
  <p><em>example.php</em></p>
  
  <pre><code>&lt;?php
  
  include 'Awards.php';
  
  $class = new Awards();
  $method =  $class-&gt;connect();
  echo json_encode($method);
  </code></pre>
  
  <p><em>Awards.php</em></p>
  
  <pre><code>&lt;?php
  
  require_once 'mongodb-library/libraries/Mongo_db.php';
  include 'mongodb-library/libraries/Mongo_db.php';
  
  class Awards extends Mongo_db 
  {
  
      //put your code here
  
      public function __construct() 
      {
          parent::__construct();
      }
  
      public function connect()
      {
          $this-&gt;load();
  
  
          //The following code is used for confirmation
          $array = array(
          $this-&gt;message =&gt; '1'
          );
          return $array;
      }
      public function create()
      {
          //Code to read
      }
  
      public function read()
      {
          //Code to read
      }
  
      public function update()
      {
          //Code to update
      }
  
      public function delete()
      {
          //Code to delete
      }
  }
  </code></pre>
  
  <p>The purpose of my project is to perform CRUD operations on my MongoDB database called, test. I am using the Codeigniter-MongoDB-Library to connect to this database. I have edited the mongodb.php library file accordingly to connect to my database. The config settings for this file are given as follows:</p>
  
  <p><em>mongodb.php</em></p>
  
  <pre><code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  $config['default']['mongo_hostbase'] = 'localhost:27017';
  $config['default']['mongo_database'] = 'test';
  $config['default']['mongo_username'] = '';
  $config['default']['mongo_password'] = '';
  $config['default']['mongo_persist']  = TRUE;
  $config['default']['mongo_persist_key']  = 'ci_persist';
  $config['default']['mongo_replica_set']  = FALSE;
  $config['default']['mongo_query_safety'] = 'safe';
  $config['default']['mongo_suppress_connect_error'] = TRUE;
  $config['default']['mongo_host_db_flag']   = FALSE;
  </code></pre>
  
  <p>The problem here is that the connect function is not getting executed; in other words, I am not able to connect to my database successfully. How do I know this? Because on the awards.html page I am getting an output which reads, No direct script access allowed, which is coming from the mongo_db.php file (mind you not the mongodb.php file I have mentioned above).</p>
  
  <p>Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.</p>