PHP Lesson 2 - Processing Forms

May 29, 2008

In this lesson you will learn how to use PHP for processing a form that could be used in real-life scenarios.



Create a Form

  1. Open the index.php file that you created in lesson 1.

  2. Erase everything.

  3. Copy and paste the following into your file and save it.


<form action="form_process.php" method="post">
<input type="text" name="FirstName">
<input type="submit" value="Submit">
<form>

Create a Page to Process the Form

  1. In the same folder as the index.php file, create a new file called form_process.php.

  2. Type the following into the new file and save it.


Your first name is <?php echo $_POST['FirstName']; ?>.

Lesson

  • Because you typed method="post" in the tag on the first page, whatever you type in and send for the FirstName field gets stored in the php array called $_POST.

  • If you type method="get" in the form tag, then the field values will be put into the php variable called $_GET.

  • If on the form_process page you don't care whether the field values come through the $_GET or $_POST arrays then you can access them through the php array called $_REQUEST. For example: $_REQUEST['FirstName']. The $_REQUEST array contains any URL parameters, any Form parameters, and any Cookie variables.


Lesson Index
Anthony Tietjen

About the Author

Anthony Tietjen is a software engineer with experience across desktop, mobile, and web development. Husband and father, with a passion for music and the outdoors. Connect with Anthony on LinkedIn and X/Twitter