1. Go to this page and download the library: Download coercive/ajax library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
coercive / ajax example snippets
use Coercive\Utility\Ajax\Response;
# Example of loading class Response
$response = new Response;
# Configuration options
// Deactivading the auto skip null parameters (and empty array)
// Useful for full params export
$response->skipNull(false);
// Require only certain fields
$response->Codes(200, 200);
// Reset all parametters, but not the defaults options
$response->reset();
// It's possible to list fields to keep
$response->reset(['id', 'name', 'config']);
use Coercive\Utility\Ajax\Response;
$response = new Response;
# Get full export params as array
$response->array();
# Get full export params as json string
$response->json();
# Auto export as json string
$response->__toString();
# Send as json string with clear buffer and http codes options
$response->send();
# Httpcodes setted here temporary overrides the setted codes with httpResponseCodes() method
$response->send(true, 200, 500);
use Coercive\Utility\Ajax\Response;
$response = new Response;
# Default options
$response->setDefaultId('Hello123');
// This default option will not be reseted by reset() method.
# Associated custom parameter
$response->setId('World456');
// If id not setted, the default id will be used
use Coercive\Utility\Ajax\Response;
$response = new Response;
# Some default parameters has status handler
$response
->setDefaultMessage('Default message for all status !');
# But you may wan't to have separate text in case of success or failure status
$response
->setDefaultSuccessMessage('Default message for success status only !')
->setDefaultFailureMessage('Default message for failure status only !');
use Coercive\Utility\Ajax\Response;
$response = new Response;
# Examples
$response
->setStatus(true)
->setLanguage('EN')
->setType('popup')
->setTitle('Alert')
->setMessage('This is an example message')
->setClose(true)
->setCloseLabel('ok')
->setDelay(3000);
// etc...
use Coercive\Utility\Ajax\Response;
$response = new Response;
# Global setter
$response->set('entry_name', [
'key' => 'value'
]);
$response->set('other_entry_name', 'value');
# Example merge
$response->merge('entry_name', [
'additional_info' => 'value'
]);
# Example insert
$response->insert('entry_name', 'forgoten', 'value');
# Example add, automatic array if several elements with the 'add...' method
$response->add('array_element', 'example_key', 'content1');
$response->add('array_element', 'example_key', 'content2');
$response->add('array_element', 'example_key', 'content3');
// array_element: {
// example_key: [
// "content1",
// "content2",
// "content3"
// ]
// }
# Example drop parameters
$response->set('options', [
'otp1' => '1111',
'otp2' => '2222',
'otp3' => '3333',
]);
$response->drop('options', [
'otp2' => '2222',
'otp3' => '3333',
'otp4' => '4444',
'otp5' => '5555',
]);
$response->remove('options', 'otp1');
# Example getter
$value = $response->get('key');
$value = $response->target('array_element', 'example_key');
$notExist = $response->get('notExist', 'default_value');
$notExist = $response->target('array_element', 'notExist', 'default_value');
use Coercive\Utility\Ajax\Response;
$response = new Response;
$response->callAfterArray(function ($arr) {
var_dump($arr); # do something
});
$response->callAfterJson(function ($json) {
var_dump($json); # do something
});
use Coercive\Utility\Ajax\Response;
$response = new Response;
# As datas, configs, options, texts
# The 'logs' parameter can be use for a JS console.log() for example
$response->setLogs([
'test' => '{}"#&)°',
]);
# And there is some JSON compilation error getters
if($response->getJsonLastErrorStatus()) {
echo $response->getJsonLastErrorCode();
echo $response->getJsonLastErrorMessage();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.