This library facilitates working with the jQuery framework from the server-side.
However, it's important to note that this approach is considered an antipattern. Server-side code typically shouldn't have knowledge of HTML structure or manipulate the DOM in this manner.
- PHP 7.4.0 or higher (with JSON extension)
- jQuery 1.10 / 2.2 / 3.3 or higher
Install Composer in Your Project.
Run this in your command line:
curl -sS https://getcomposer.org/installer | phpOr download composer.phar into your project root.
Execute this in your project root.
php composer.phar installAdd the autoloader to your PHP file.:
require 'vendor/autoload.php';After this, you can use this jQuery-PHP library in your PHP code, for example, this is index.php:
use function AntonShevchuk\jQuery\jQuery;
use function AntonShevchuk\jQuery\jQueryContainer;
// create jQuery-way queries
jQuery('#demo')
->html('Server time: ' . date('H:i:s'))
->css('backgroundColor', '#ffffdd')
->animate(['opacity' => 'show'], 3000)
->animate(['opacity' => 'hide'], 3000)
;
// return JSON
print json_encode(jQueryContainer());Include jQuery and jQuery-PHP library:
<!-- Load jQuery -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<!-- Load main jQuery-PHP scripts -->
<script src="public/js/jquery.php.js"></script>Add a call in your HTML page:
<!-- Call $.php -->
<button onclick="$.php('index.php',{'action':'demo'});return false;">Run demo snippet</button>
<!-- Target DOM Element -->
<div id="demo">...</div>You can find more examples on the site: https://jquery.hohli.com
The project is developed by Anton Shevchuk, and distributed under MIT LICENSE