Using HTML Form Variables with PHP

Using HTML Form Variables with PHP
In this tutorial, we will take a look at how to pass the value of a variable from a webpage to a PHP program. Essentially, there are two parts to this. The first part is collecting information from your website visitor and passing that information to the processing program. The second part is coding the processing program to “recognize” each variable and the information it carries. What happens to the information then depends on the purpose of the program. Let’s follow along with a couple example variables as they make their way from the webpage to a processing program called program.php.

The easiest way to collect information from your visitors is to use an HTML form. Each piece of information that you wish to collect is assigned to a program variable and each variable is given a unique name. For our example, we will be asking the visitor is give us their first name and email address.

  1. First, we need to give a unique name of each variable. Let’s make it easy to tell them apart by giving each variable a name that suggests what information it will hold. We will call the first name variable “first_name” and the email variable “email_address”. Next, we will use the HTML input tag to create the input fields in the web browser. Notice that the value of each variable is initially set to null (value="") inside the tags.

    <input type="text" name="first_name" value="" size="30" maxlength="10">

    <input type="text" name="email_address" value="" size="30" maxlength="10">

  2. We need to send the collected information to the processing program called program.php. We can do this with the action attribute in the HTML form tag.

    <form method="post" action="program.php">

  3. Of course, we will need a Send button to send the form on its way to the program.

    <input type="submit" value="Send">

  4. Now, the program needs to recognized the information. You can think of it as checking to see if the information made its way to the program. The first section of code below says “if the first_name variable passed from the web form,called $_POST['first_name'], is NOT (!) empty, then set the value of $first_name to the value of $_POST['first_name']. The second section of code does the same for the email_address variable.

    $first_name = $_POST['first_name'];

    $email_address = $_POST['email_address'];

    Now, the two variables $first_name and $email_address are ready to be used in the rest of the program.

    Note–When asking for personal information such as an email address, you should build security features into your program that will protect your visitor’s information.

This site needs an editor - click to learn more!

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.