PHP Forms Processing Form Input - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Forms Processing Form Input - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 1, 2019

PHP Forms Processing Form Input

PHP Forms




Processing Form Input

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.

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   Deciding what to do based on request method

           <?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
              <form action="<?php echo htmlentities($_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'] . '!';
           }

Discussion

Forms can be easier to maintain when all parts live in the same file (or are referenced by 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. 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.

If you’re using a web application framework, it has its own conventions on how you mix displaying a form and processing the results. While we don’t focus on any specific framework in this book, having a separation between the presentation part of your application (showing things to users) and the “business logic” part of your application (doing stuff with the data users give you) is a good idea to keep your code maintainable and easy to understand. If your form is anything more complicated.

you can benefit from splitting out the display logic into a template. There are lots of swell template languages but to keep things simple in this book we use PHP itself as the template language.

Reworked this way, becomes three files: one that displays the form on a get request, one that processes the results on a post request and one that decides what to do. Here’s the form display code:

           <form action="<?= htmlentities($_SERVER['SCRIPT_NAME']) ?>" method="post">
           What is your first name?
           <input type="text" name="first_name" />
           <input type="submit" value="Say Hello" />
           </form>

Here’s the form processing logic:

           Hello, <?= $_POST['first_name'] ?> !
           
And here’s the logic that decides what to do:
           
           if ($_SERVER['REQUEST_METHOD'] == 'GET') {
                include __DIR__ . '/getpost-get.php';
           }
           else {
                include __DIR__ . '/getpost-post.php';
           }

The deciding-what-to-do logic assumes that the form display code is saved as getpost-get.php, that the form processing code is saved as getpost-post.php and that all three files are in the same directory. The __DIR__ constant tells the program to look in the same directory as the executing code for the files being included.

We’ll use this strategy of breaking things out into separate files in other recipes.


No comments:

Post a Comment

Post Top Ad