Checking Whether a Form Has Been Submitted


 if (isset($_POST['Submit'])) { ... } else { ... } 


When both the HTML form and the processing PHP code are on the same page (something that is recommended when it comes to prefilling forms), it is important to find out whether a form is just called in the browser (using GET) or if the form is submitted (and the data must be processed).

Checking Whether a Form Has Been Submitted (submit.php; excerpt)
 <?php   if (isset($_POST['Submit'])) { ...     echo '<h1>Thank you for filling out this form!       </h1>';   } else { ... ?>   <form method="post" action="<?php echo      htmlspecialchars($_SERVER['PHP_SELF']); ?>">     <input type="submit" name="Submit" value="Submit       form" />   </form> <?php   } ?> 

You can take several different approaches to this task; something that always works is assigning the Submit button a name and then testing whether this name is present when the form is submitted.

TIP

If your form has multiple Submit buttons, give each of them a distinctive name; then, you can check specifically for this name and, therefore, determine which Submit button has been used.





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net