A lightweight PHP utility to simplify HTMX integration into PHP projects.
This helper provides clean, expressive functions for HTMX-specific headers and server-side logic — perfect for building modern interactive web applications without writing JavaScript.
🧪 Project repo: github.com/makalin/htmxphp
✅ Detect HTMX requests
✅ Trigger client-side events
✅ Redirects, URL push/replace
✅ JSON, HTML, No-content responses
✅ DOM swapping helpers
✅ Client-side navigation & refresh
✅ Simple function-based API
wget https://github.com/makalin/htmxphp/main/htmx.php
or
git clone https://github.com/makalin/htmxphp.git
Then require the file in your PHP code:
require_once 'htmx.php';
is_htmx_request(): bool
is_htmx_boosted(): bool
get_htmx_trigger(): ?string
htmx_trigger(string $eventName, array $data = [])
htmx_trigger_multiple(array $events)
htmx_push_url(string $url)
htmx_replace_url(string $url)
htmx_redirect(string $url)
htmx_location(string $url, string $target = "body", string $swap = "innerHTML")
htmx_refresh()
htmx_header(string $key, string $value)
htmx_status(int $code)
htmx_html(string $html)
htmx_json(array $data)
htmx_nocontent()
htmx_replace(string $html)
htmx_raw(string $data)
require_once 'htmx.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$msg = trim($_POST['message'] ?? '');
if ($msg) {
file_put_contents("log.txt", $msg . PHP_EOL, FILE_APPEND);
htmx_trigger('messageReceived', ['text' => $msg]);
htmx_nocontent();
} else {
htmx_status(400);
htmx_html("<div class='error'>Message is required.</div>");
}
}
- Modern form submissions
- Realtime content updates
- Dashboards & activity feeds
- Interactive UIs without JavaScript frameworks
- PHP 7.2+
- HTMX frontend library
MIT License © 2025 Mehmet Turgay AKALIN
If this helped you, consider starring the project on GitHub:
👉 https://github.com/makalin/htmxphp