There are two common ways to send a request from an HTML page using the http method other than GET or POST.
# 1 : use the html form to submit a POST request, but specify a hidden form field that tells the server to process the request as if it were using a different method. This is the approach indicated by @xdazz .
<form method="post" action="my_resource.php"> ... <input type="hidden" name="REQUEST_METHOD" value="PUT" /> <form>
In your PHP script, "my_resource.php" you will need to look at both the real request method and the submit form field to determine which logic to call:
/* my_resource.php */ $method = strtolower($_SERVER['REQUEST_METHOD']); if( $method === 'post' && isset($_REQUEST['REQUEST_METHOD'])) { $tmp = strtolower((string)$_REQUEST['REQUEST_METHOD']); if( in_array( $tmp, array( 'put', 'delete', 'head', 'options' ))) { $method = $tmp; } unset($tmp); } // now, just run the logic that appropriate for the requested method switch( $method ) { case "get": // logic for GET here break; case "put": // logic for PUT here break; case "post": // logic for POST here break; case "delete": // logic for DELETE here break; case "head": // logic for DELETE here break; case "options": // logic for DELETE here break; default: header('HTTP/1.0 501 Not Implemented'); die(); }
Note. You can put the above logic on each page (or call it from each page). An alternative is to create a proxy script (for example, "rest-form-proxy.php" ). Then all forms on your site will be sent to the proxy server, including request_method, and the destination URL. The proxy will retrieve the provided information and redirect the request to the required URL using the appropriate requested http method.
A proxy approach is a great alternative to embedding logic in every script. If you are still creating a proxy server, be sure to check the requested URL and do not allow a URL that does not point to your own site. Failure to perform this check will allow other users to use your proxy server to launch malicious attacks on other sites; and it may also compromise the security and / or privacy of your website.
-
# 2 Use Javascript on your HTML page to trigger XMLHttpRequest . This is a more complex approach that requires a bit of javascript, but in some cases it can be more flexible. It allows you to send requests to the server without reloading the page. It also allows you to send data in many different formats (you are not limited to sending only data from an html form). For instance:
<button onclick="doSave()">Save</button> <script> var myObject = { </script>
XMLHttpRequest is much larger than the main example above. If you choose this route, please study it carefully. Among other things, make sure that you handle the various error conditions correctly. There are also a number of compatibility issues between browsers, many of which can be resolved using an intermediary, such as the jQuery $ .ajax () function .
Finally, I must note that the two above methods are not mutually exclusive. It is possible to use forms for some requests and XMLHttpRequest for others if you create your own server so that it can handle any kind of request (as shown in No. 1 above).