How to test if a form is submitted?

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") 
Tags: 

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.