Recipe 9.1. Processing Form Input


9.1.1. Problem

You want to use the same HTML page to emit a form and then process the data entered into it. In other words, you're trying to avoid a proliferation of pages that each handle different steps in a transaction.

9.1.2. Solution

Use the $_SERVER['REQUEST_METHOD'] variable to determine whether the request was submitted with the get or post method. If the get method was used, print the form. If the post method was used, process the form. Example 9-3 combines the form from Example 9-1 and the code from Example 9-2 into one program, deciding what to do based on $_SERVER['REQUEST_METHOD'].

Deciding what to do based on request method

<?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?> <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> What is your first name? <input type="text" name="first_name" /> <input type="submit" value="Say Hello" /> </form> <?php } else {     echo 'Hello, ' . $_POST['first_name'] . '!'; } ?>

9.1.3. Discussion

Back in the hazy past, in the early days of the Web, when our ancestors scratched out forms, they usually made two files: a static HTML page with the form and a script that processed the form and returned a dynamically generated response to the user. This was a little unwieldy because form.html led to form.cgi and, if you changed one page, you needed to also remember to edit the other, or your script might break.

Usually, forms are easier to maintain when all parts live in the same file and context dictates which sections to display. The get method (what your browser uses when you just type in a URL or click on a link) means "Hey, server, give me something you've got." The post method (what your browser uses when you submit a form whose method attribute is set to post) means "Hey, server, here's some data that changes something." So the characteristic response to a get request is the HTML form, and the response to the post request is the results of processing that form. In Example 9-3, the "processing" is extremely simple'just printing a greeting. In more typical applications, the processing is more complicated'saving information to a database or sending an email message.

Note that although the XHTML specification requires that the method attribute of a <form/> element be lowercase (get or post), the HTTP specification requires that a web browser use all uppercase (GET or POST) when sending the request method to the server. The value in $_SERVER['REQUEST_METHOD'] is whatever the browser sends, so in practice it will always be uppercase.

One other technique also makes pages easier to maintain: don't hardcode the path to your page directly into the form action. This makes it impossible to rename or relocate your page without also editing it. Instead, use the $_SERVER['SCRIPT_NAME'] variable as the form action. This is set up by PHP on each request to contain the filename (relative to the document root) of the current script.

9.1.4. See Also

Recipe 9.11 for handling multipage forms.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

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