I’m looking for a clean way to handle forms with PHP that does validation and caching of data, but doesn’t require mixing PHP with the form HTML. Basically, I want to be able to write a form in pure HTML and intercept that form on it’s way to it’s destination, process it and either let the data go happily on it’s way, or return to the form sending validation errors and the cached results back with it.
This all part of a little framework I’m developing as an aid to learning. My initial plan was to use the Routing object to sniff out GET and POST data, but that data doesn’t always indicate the presence of a form.
I’ve been looking through this list of HTTP Headers but I’m not seeing anything that could help. Although I’ve never bothered looking at HTTP Headers before so I could easily be missing something.
Or is there some other reliable way of detecting form submissions?
There is no generic way to distinguish between form submissions and simple links when it comes to GET requests.
With plain HTML, only forms can generate POST requests, so filtering on REQUEST_METHOD might be a viable solution. (However, this obviously won’t catch GET requests, and AJAX can also generate POST requests, so…)
If you control the form, I’d suggest adding something like
to every form. (For bonus points, you can intercept the form HTML and add the above line on the fly.)