posted by scott
on Thu, 02/16/2012 - 00:57
We live in a very busy world, and sometimes we try to get things done quick-and-dirty. One of the most popular example is to create a PHP script that renders a view and also handles form submission (action). So, what is the best way to test if a form has been submitted?
For example, let's look at the HTML form snippet below:
<form name="example" action="example.php" method="post"> <input type="text" name="name" /> <input type="submit" name="submit" value="Submit"> </form>
Most PHP programmers tend to use the isset() method to test a form submission. You may use isset($_POST['submit']), which works perfectly fine but what happens if you use image button to submit a form? For example,
<form name="example" action="example.php" method="post"> <input type="text" name="name" /> <input type="image" name="submit" src="submit.png" id="submit" value="submit" /> </form>
If you use isset($_POST['btnSubmit']) to test form submission, the test will FAIL in the above example. The best way to test form submission is to use REQUEST method.
if ($_SERVER['REQUEST_METHOD'] == "POST")
Comments
Add new comment