Handling HTML Forms with PHP – Free PHP Tutorials

When we talk about building real-world applications with PHP we have to deal with the real-time data i.e. the user input,  and a key part of most PHP applications are the ability to accept input from the person using the application.One of the most common ways to receive input from the user of a Web application is via an HTML
form.
Common examples include contact forms that let you email a site owner; order forms that let you order products from an online store; and Web – based email systems that let you send and receive email messages using your Web browser.

How HTML Forms Work

Before looking at the PHP side of things, take a quick look at how an HTML form is constructed. An HTML form, or Web form, is simply a collection of HTML elements embedded within a standard
Web page. By adding different types of elements, you can create different form fields, such as text fields, pull – down menus, checkboxes, and so on.

All Web forms start with an opening < form > tag, and end with a closing < /form > tag:

< form action=”myscript.php” method=”post” >
< !-- Contents of the form go here -- >
< /form >

Notice that there are two attributes within the opening < form > tag:
action tells the Web browser where to send the form data when the user fills out and submits the form. This should either be an absolute URL (such as http://www.example.com/myscript.php ) or a relative URL (such as myscript.php , /myscript.php , or ../scripts/myscript.php ). The script at the specified URL should be capable of accepting and processing the form data; more on this in a moment. method tells the browser how to send the form data. You can use two methods: get is useful for sending small amounts of data and makes it easy for the user to resubmit the form, and post can send much larger amounts of form data.
Once you ‘ve created your basic form element, you can fill it with various elements to create the fields and other controls within your form (as well as other HTML elements such as headings, paragraphs, and tables, if you so desire).


Capturing Form Data with PHP

You now know how to create an HTML form, and how data in a form is sent to the server. How do you write a PHP script to handle that data when it arrives at the server?
First of all, the form ’ s action attribute needs to contain the URL of the PHP script that will handle the form. For example:

< form action=”form_handler.php” method=”post” >

Next, of course, you need to create the form_handler.php script. When users send their forms, their
data is sent to the server and the form_handler.php script is run. The script then needs to read the
form data and act on it.
To read the data from a form, you use a few superglobal variables. A superglobal is a built – in PHP variable that is available in any scope: at the top level of your script, within a function, or within a class method.

$GLOBALS – superglobal array, which contains a list of all global variables used in your applications. Here, you learn about three new superglobal arrays:

1.$_GET: Contains a list of all the field names and values sent by a form using
the get method.

2.$_POST: Contains a list of all the field names and values sent by a form using
the post method.

3.$_REQUEST: Contains the values of both the $_GET and $_POST arrays combined,
along with the values of the $_COOKIE superglobal array.

Each of these three superglobal arrays contains the field names from the sent form as array keys, with
the field values themselves as array values. For example, say you created a form using the get method,
and that form contained the following control:

< input type=”text ” name=”emailAddress” value=”” / >

You could then access the value that the user entered into that form field using either the $_GET or the $_REQUEST superglobal:

<?php

$email = $_GET[“emailAddress”];
$email = $_REQUEST[“emailAddress”];
?>

For more details, follow the Padhle.com PHP tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *