Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home Forms


Form PHP
Form PHP

     PHP Forms and User Input

 

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

A very common application of PHP is to have an HTML form gather information from a website's visitor and then use PHP to do process that information.

 

  • PHP Form Handling



The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

Example:

The example below contains an HTML form with two input fields and a submit button. You can save it like "form.html".

Code:

                                              <html>
                                              <body>

                                              <form action="welcome.php" method="post">
                                              Name:  <input type="text" name="fname" />
                                              Age:    <input type="text" name="age" />
                                                         <input type="submit" />
                                              </form>

                                              </body>
                                              </html> 

 

 

When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called "welcome.php":

"welcome.php" looks like this:

Code:

                                              <html>
                                              <body>

                                              Welcome <?php echo $_POST["fname"]; ?>!<br />
                                              You are   <?php echo $_POST["age"]; ?> years old.

                                              </body>
                                              </html> 

 

 As you probably noticed, the name in $_POST['name'] corresponds to the name that we specified in our HTML form.

 An output wiil be:

                                         Welcome here is the name that you type in the form!

                                         You are here is the age that you type in the form years old.

 

 

A lot of things were going on in this example. Let us step through it to be sure you understand what was going on.

  1. We first created an HTML form "form.html" that had two input fields specified, "fname" and "age" and a submit button.
  2. We added two attributes to the form tag to point to "welcome.php" and set the method to "post".
  3. We used the PHP echo function to output($_POST) the visitors name and ages.

 

After all it wasn't to much.

Remember, this lesson is only to teach you how to use PHP to get information from HTML forms.

 

  • Form Validation


User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load.

You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.

 

 

    PHP $_GET