O'Reilly logo

PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition by Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling HTML Forms with PHP Redux

All of the examples in this chapter and the preceding one used two separate scripts for handling HTML forms: one that displayed the form and another that received it. While there’s certainly nothing wrong with this method, there are advantages to having the entire process in one script. To have one page both display and handle a form, use a conditional.

if (/* form has been submitted */) {
   // Handle it.
} else {
   // Display it.
}

To determine if the form has been submitted, I normally check that a $_POST variable is set (assuming that the form uses the POST method, of course). For example, you can check $_POST['submitted'], assuming that’s the name of a hidden input type in the form.

if (isset($_POST['submitted'])) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required