bluepost / blueajax
Super-Simple framework for making AJAX easier to deal with on the PHP side
Installs: 31
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
pkg:composer/bluepost/blueajax
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2020-12-09 17:44:53 UTC
README
This lib has moved to become part of the larger BlueUtils and is not being maintained. It will be removed soon.
BlueAjax
Super-Simple framework for making AJAX easier to deal with on the PHP side. It also includes some JS objects for communicating with an AJAX php script built using this lib.
##Getting Started
Installation with Composer (Recommended)
We recommend you install the package with Composer:
composer require bluepost/blueajax
Installation
Alternatively you can download the Latest Release, unzip it, and upload the src folder to your environment. Then require it for every file for which you need it.
require_once ('BlueAjax.php');
JavaScript
The javascript components are all in JS/BlueAjax.js file.
TODO
- Make all AjaxResponse functions use respond()
Using BlueAjax
The framework is set up to use "success" and "error" to communicate data. Both the php and js sides rely on this. The data is a string, so we recommend that you use them to communicate the status of the request. This allows for human readable response messages without an unnecessary booleans being passed back and forth.
<?php require_once '../BlueAjax.php'; //Create a response object $response = new BluePost\AjaxResponse(); //Check if the GET index "q" isset, if not, die with an error: '{"error":"q not set"}' $response->assert(GETisset("q"), error("q not set")); //Check if $_GET["q"] is a correct value (1 or 2), if not die with an error: '{"error":"q invalid"}' $response->isGoodVal($_GET["q"], [1,2], error("q invalid")); //If $_GET["q"] is 1, then make sure that the GET index "text" is set. If not, die with an error: '{"error":"text not set"}' $response->condAssert($_GET["q"] == 1, GETisset("text"), error("text not set")); //Respond with an "All good!" message $response->respond(success("All good!"));
<script src = "https://code.jquery.com/jquery-2.2.1.min.js"></script> <script src = "/JS/BlueAjax.js"></script> <script> var req = new GetAjaxRequest ("/src/tests/test.php", {"q": 1, "text":"Text here"}) .onError(function (f) {console.log(f)}) .onSuccess(function (s) {console.log(s)}) .execute() var AjaxRequest = AjaxRequestFactoryFactory("/src/tests/", //OnError function (f) {console.log(f)}, //OnSuccess function (s) {console.log(s)} ); var req = AjaxRequest("test.php", {"q": 1, "text":"Text here"}).execute() </script>
API Details
PHP
BluePost\AjaxResponse
__construct($baseArray = Array())- The constructor function takes an optional
$baseArraywhich will form the inner response object (seerespond,addResponse)
- The constructor function takes an optional
assert($test, $error, $json = TRUE)- If
$testis FALSE then the function will die with$error. If$jsonis TRUE, then the function will json_encode$error.
- If
isGoodVal($needle, $haystack, $error, $json = TRUE)- If
$needleisn't in$haystackthe function will die with$error. If$jsonis TRUE, then the function will json_encode$error.
- If
isGoodKey($needle, $haystack, $error, $json = TRUE)- If
$needleisn't a valid key for the array$haystackthe function will die with$error. If$jsonis TRUE, then the function will json_encode$error.
- If
condAssert($cond, $test, $error, $json = TRUE)- If
$condis true then the function will callassert($test, $error, $json).
- If
addResponse($key, $value)- This will add the key-value pair to the inner response object which is used when the respond function is called.
respond($response=Array(), $add=TRUE)- This will die with the json_encoded value of
$responsemerged with the inner response array (seeaddResponse). If$addis False then the inner array won't be used.
- This will die with the json_encoded value of
Isset Functions
GETisset($index, $harsh = TRUE)- This function will return TRUE if
$_GET[$index]isset. If$harshis TRUE then the function will usestrictIsset
- This function will return TRUE if
POSTisset($index, $harsh = TRUE)- See
GETissetbut with $_POST
- See
strictIsset($test, $notZero = FALSE, $notEmptyString = TRUE, $notNull = TRUE)- An isset that has a number of extra tests, controlled by the boolean variables
laxIsset ($test, $NES = FALSE)- This uses
strictIssetwith all optional params set to FALSE, except$notEmptyString, which is set to$NES
- This uses