Home arrow PHP arrow PHP Cross Browser Compatibility: How to Check if a Web Form is Submitted

PHP Cross Browser Compatibility: How to Check if a Web Form is Submitted

This is a tutorial targeted to beginners who need to know the best way of checking if a web form is submitted using PHP. The majority of PHP web applications are form handling tasks, and it is vital that you know the different ways of checking form submission.

TABLE OF CONTENTS:
  1. PHP Cross Browser Compatibility: How to Check if a Web Form is Submitted
  2. The Test Environment for Cross Browser Compatibility
By: Codex-M
Rating: starstarstarstarstar / 5
December 28, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

With this knowledge, you can choose what is appropriate for your project, and select the best method for cross browser compatibility. Remember that not all situations/methods will work in all browsers, and it is important that you select a method that will be compatible in most major browsers and in different operating systems.

At the end of this tutorial, recommended methods will be proposed based on the results of cross browser testing.

The Different Methods for Checking Form Submission

Doing research with PHP-related websites, textbooks and forums on the Internet turns up around six different techniques you can use to check if a web form is submitted using PHP.

Some of these methods rely on the submit button HTML name attribute. For example, it is common for developers to just use:

<input type="submit" value="Submit this form">

The above does not use the name attribute. Sometimes developers will use the name attribute for the submit button:

<input type="submit" name="submit" value="Submit this form">

However, most developers, particularly beginners, are not aware that the presence and absence of this HTML button name attribute can make a huge difference in your PHP web form application if you decide to use a method that depends on these.

NOTE: It is highly important to adhere to correct HTML coding and it is recommended that you set the name attribute in your HTML submit button.

Again, with each of these six methods, the developer can either use the submit button name attribute or not. Let's assign "B" to those methods that do not use the submit name attribute, and "A" to those that use the name attribute.

Therefore, these six methods are:

First method:  if (!$_POST['submit'])

Method1A structure:

<?php
if (!$_POST['submit'])
{
//form is not submitted
?>
//Show HTML form
<input type="submit" name="submit" value="Submit this form">
<?php
}
else
{
//form submitted
//do form processing and validation
}
?>

Method 1B still uses if (!$_POST['submit']) but the submit button HTML code does NOT have the name attribute:

<input type="submit" value="Submit this form">

Second method:  if (!isset($_POST['submit']))

Method2A structure:

<?php
if (!isset($_POST['submit']))
{
//form is not submitted
?>
//Show HTML form
<input type="submit" name="submit" value="Submit this form">
<?php
}
else
{
//form submitted
//do form processing and validation
}
?>

Method 2B does not have the name attribute in the submit button.

Third method: if (!isset($_POST['fieldname']))

This method checks to see if one of the form field names is set. This is different from the second method, which checks to see if the submit attribute has been set.

Method 3A structure:

<?php
if (!isset($_POST['fieldname']))
{
//form is not submitted
?>
//Show HTML form
Enter text: <input name="fieldname" size="20">
<input type="submit" name="submit" value="Submit this form">
<?php
}
else
{
//form submitted
//do form processing and validation
}
?>

Method 3B has the same structure as Method3A, except that the submit button does not have a name attribute.

Fourth method: if(!($HTTP_SERVER_VARS['REQUEST_METHOD']=='post'))

This method can be implemented with the same structure as the previous methods. Method 4A does use a name attribute in the submit button, while Method 4B does not.

Fifth method: if (!(array_key_exists('_submit_check',$_POST)))

This is a special method because it implements a hidden attribute, whose structure is shown below:

<?php
if (!(array_key_exists('_submit_check',$_POST)))
{
//form is not submitted
?>
//Show HTML form
<input type="hidden" name="_submit_check" value="1"/>
<input type="submit" name="submit" value="Submit this form">
<?php
}
else
{
//form submitted
//do form processing and validation
}
?>

Sixth method:  if (!($_POST))

This appears to be the simplest method. Simply check for $_POST to see if the form is submitted or not.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: