ARTICLE AD BOX
Here is an example that is self contained and provides an example of handling both standard and ajaxed type request for common http verbs. This does NOT take into security of any type. This is strictly for providing a working example of how one could detect and process both types of request. The code is pretty well commented so it should be fairly clear what this is doing. Hope it helps.
Once you enable htmx to test the ajax handling each time you make post one of the forms you will need to reload the page to reload the form.
<?php $enableHtmx = false; // Set to false to disable HTMX and ajax requests function isActionRequest( array $methods = ['POST'], // the methods to check for ?string $formControl = null, // the form control to check for ): bool { // if $enableHtmx is false and method is not POST then match will return 'GET' $method = match($_SERVER['REQUEST_METHOD']) { 'POST', 'PUT', 'PATCH' => $_SERVER['REQUEST_METHOD'], default => 'GET' }; // if we do not have a form control this is just a simple check // since we matched the method and it is in $methods to check against return true if ($formControl === null && in_array($method, $methods)) { return true; } if ($formControl !== null && in_array($method, $methods)) { // if we have the form control in the $_POST data we might as well skip out now if (isset($_POST[$formControl])) { return true; } // Forms can be submitted with GET method if (isset($_GET[$formControl])) { return true; } } // if $enableHtmx is true then we pick it up here $ajaxHandler = function() use ($method, $formControl): bool { $ajaxMethods = ['PUT', 'PATCH', 'DELETE']; $headers = getallheaders(); $contentType = $headers['Content-Type'] ?? $headers['content-type'] ?? null; $rawBody = file_get_contents('php://input'); $parsedBody = []; if ($contentType === 'application/x-www-form-urlencoded') { parse_str($rawBody, $parsedBody); } elseif ($contentType === 'application/json') { $parsedBody = json_decode($rawBody, true); } $_REQUEST = array_merge($_REQUEST, $parsedBody); return isset($parsedBody[$formControl]) && in_array($method, $ajaxMethods); }; return $ajaxHandler(); } if (isActionRequest(['POST', 'PUT', 'PATCH', 'DELETE', 'GET'], 'form-control')) { // This is a request from HTMX or a form submission // Handle the request here $formControl = $_POST['form-control'] ?? $_GET['form-control'] ?? $_REQUEST['form-control'] ?? null; $method = $_SERVER['REQUEST_METHOD']; echo "Form control: $formControl, Method: $method"; exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <?php if ($enableHtmx): ?> <script src="https://unpkg.com/[email protected]" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script> <?php endif; ?> </head> <body hx-boost="true"> POST <form method="POST" action=""> <input type="text" name="form-control" value="test"> <button type="submit">Submit</button> </form> PUT <form method="PUT" action=""> <input type="text" name="form-control" value="test"> <button type="submit">Submit</button> </form> PATCH <form method="PATCH" action=""> <input type="text" name="form-control" value="test"> <button type="submit">Submit</button> </form> GET <form method="GET" action=""> <input type="text" name="form-control" value="test"> <button type="submit">Submit</button> </form> DELETE <form method="DELETE" action=""> <input type="text" name="form-control" value="test"> <button type="submit">Submit</button> </form> </body> </html>