jQuery Icon

jQuery-PHP

jQuery-PHP is a PHP library that seamlessly integrates jQuery with PHP, eliminating the need to worry about transmission, parsing, and other tedious tasks. With this bridge, you can bring the DOM directly onto your server and easily manipulate it in a jQuery-style manner.

GitHub Icon GitHub

Installation

jQuery-PHP package

To require the jQuery-PHP package with Composer, you need to create or navigate to your project directory in your terminal or command prompt and then execute the following command:

shell Shell code
php composer.phar require anton-shevchuk/jquery-php

To require the Composer autoloader in your PHP file, you can add the following line at the top of your PHP script:

PHP PHP code
require __DIR__ . '/vendor/autoload.php';

To use functions from the jQuery-PHP package after requiring the Composer autoloader, you can call the functions directly in your PHP code. Here's an example:

PHP PHP code
use function AntonShevchuk\jQuery\jQuery;
use function AntonShevchuk\jQuery\jQueryContainer;

To create jQuery-like chain calls using the jQuery-PHP package, you can use method chaining. Here's an example of how you can chain method calls together:

PHP PHP code
// Create a chain of jQuery-like calls
jQuery('#element_id')
    ->html('Hello, world!')
    ->css('color', 'red')
    ->addClass('highlight')
    ->animate(['opacity' => 'show'], 1000)
    ->delay(2000)
    ->fadeOut();

After chaining all the method calls, you can obtain a JSON response containing all the commands. Here's how you can do it:

PHP PHP code
// get JSON response
print json_encode(
    jQueryContainer()
);

JavaScript initialization

To initialize jQuery and jQuery.php in HTML, you would typically include the jQuery library first, followed by the jQuery.php script. Here's an example:

HTML HTML code
<script src="path/to/jquery.js"></script>
<script src="path/to/jquery.php.js"></script>

Replace path/to/...js with the actual path to your jQuery and jQuery.php scripts. Make sure to place jquery.php.js script tag after including jQuery, as jQuery.php depends on jQuery being loaded first.

Then, make an AJAX call to your PHP file using jQuery and method $.php():

JS JavaScript code
// AJAX call to your PHP file
$.php('ajax.php', {'action':'demo'});

Usage

PHP PHP code
jQuery('#test0')
    ->html('Server time: ' . date('H:i:s'));
target target <div id="test0">
...
</div>

PHP PHP code
jQuery('#test1')
    ->html('Server time: ' . date('H:i:s'));
    ->css('backgroundColor', '#ffff00')
target target <div id="test1">
...
</div>

PHP PHP code
jQuery('#test2 div.red')
    ->html(date('H:i:s'))
    ->css('backgroundColor', '#ff0000');
jQuery('#test2 div.green')
    ->html(date('H:i:s'))
    ->css('backgroundColor', '#00ff00');
jQuery('#test2 div.blue')
    ->html(date('H:i:s'))
    ->css('backgroundColor', '#0000ff');
target target <div id="test2">
...
...
...
</div>

PHP PHP code
// Slide Up
jQuery('#test3')
    ->slideUp(500);

// Slide Down
jQuery('#test3')
    ->slideDown(500);
target target <div id="test3">
...
</div>

PHP PHP code
// Hide
jQuery('#test4')
    ->css('color', '#000000')
    ->animate(['opacity' => 'hide'], 1000);

// Show
jQuery('#test4')
    ->css('color', '#ff0000')
    ->animate(['opacity' => 'show'], 1000);
target target <div id="test4">
...
</div>

PHP PHP code
jQuery()->alert('Called alert() from server-side');

Call the server-script using the $.php() method, and upon receiving the result, follow the instruction to make another call to $.php() with a different action.

PHP PHP code
jQuery()->php('/ajax.php', ['action' => 'alert']);