PHP Classes

File: ajax.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Kristo Vaher  >  Smart AJAX Controller  >  ajax.php  >  Download  
File: ajax.php
Role: Example script
Content type: text/plain
Description: AJAX backend file that jQuery submits data to and waits for a response from
Class: Smart AJAX Controller
Generate AJAX request responses in JSON format
Author: By
Last change:
Date: 4 years ago
Size: 1,454 bytes


Class file image Download

//including the class file
//Initializing the controller
    //Output buffer will be used to later on return the result in gzip
$ajax=new SmartAjaxController();
//If AJAX has submitted a serialized string to be parsed to variables.
//These variables can be used by $ajax->getVar funtion

//This is dependant on javascript front-end controller, used as an example
if(!empty($_POST) && isset($_POST['action'])){
//this assumes that action is defined by jQuery before being submitted to back-end
//This is the same example used in javascript file
case 'alertLowercaseString':
$ajax->setKey('message','Your string lowercased: '.strtolower($ajax->getVar('string')));

//If no action is found, execute frontend error callback
$ajax->setKey('message','This system action does not exist!');

    } else {
//Since no action was defined or no post was used at all, we simply set the response to return errors to front-end
$ajax->setKey('message','This system action does not exist!');
//Here we return the result in gzipped JSON string
    //This also sets proper headers to the file