Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Working with Forms in PHP – Part 1

Working with Forms in PHP – Part 1

OCT 28

Forms are everywhere on the Web, you use them to enter your billing details while shopping on-line, writing a comment on a blog, or while preforming a search. Regardless, forms are essential to making the Web interactive. In this tutorial we’ll show you how to use PHP to process HTML forms. A prerequisite for this tutorial is a basic knowledge of HTML forms, a good reference is Htmlgoodies.

In simplest terms, a form is a mechanism used to gather data from a user and we can use PHP to process that data. We’ll start with a very basic example:

<html>
<head>

<title> A Simple Form </title>
</head>
<body>
	<form action="process.php" method='POST'>
	<label>Enter your first name:</label>

	<input type="text" name="first_name" size="20">
	<input type="submit" value="Submit">
	</form>
</body>
</html>

Save this into an HTML file and open it with your web browser, you should get a form similar to this:

simple HTML form example

Now that we have the form the next thing we need to do is process it. Notice the action of the form is “process.php”, when the user hits the submit button the web server invokes the process.php script and passes the form data to that script using the POST method. In the process.php script, we can do a number of different things with the data, but for this example we’ll simply display the value entered in the form field.

<?php
	echo $_POST['first_name'];
?>

You can enter any value into the form and the script will simply echo that back to the browser, this is a very crude example, but as you’ll see it demonstrates how PHP accesses form data.

Superglobals

The $_POST array is a built-in superglobal, which is a predefined array that is accessible from anywhere within a script. Superglobals provide scripts with data that is created from external sources, some of these include data from the web server, the user’s web browser, and in this case data from HTML forms. Form data can be accessed from three different superglobal arrays $_POST, $_GET, and $_REQUEST depending on the method used to submit the form. Since we used the POST method in our form, the form data will be stored and accessed from the $_POST array. Notice the index of the $_POST array is the same as the name of our form field, so in turn to access other form fields you can simply reference the superglobal followed by the form field’s name in brackets.

The $_GET superglobal can access data that was passed using the GET method. Regardless of the form submission method, you can always access form data using the $_REQUEST array. If we substitute the following in our example above, we’ll get the same results:

echo $_REQUEST['first_name'];

Our previous example is likely not going to impress anyone, so lets build on this and make it a little more interactive. In this example we use conditional statements to display different messages depending on the user’s response. We’ll start with the HTML form:

<html>
<head>
<title> A Simple Form </title>
</head>

<body>
	<form action="process.php" method='POST'>

	<label>Enter your first name:</label>
	<input type="text" name="first_name" size="20">

	<p>Choose a Color:</p>

	<label>Red</label><input type="radio" name="color" value="red"><br>

	<label>Yellow</label><input type="radio" name="color" value="yellow"><br>
	<label>Green</label><input type="radio" name="color" value="green"><br>

	<label>Orange</label><input type="radio" name="color" value="orange"><br>

	<input type="submit" value="Submit">
	</form>
</body>
</html>

simple HTML form example 2

Here is the process.php script:

<?php

if (!empty($_REQUEST['first_name']) && !empty($_REQUEST['color'])) {

	$name = $_REQUEST['first_name'];
	$color = $_REQUEST['color'];

	echo "Hi $name you choose the color: $color <br>";

	switch($color) {
		case "red" :
		  echo '<p>Red is a color of strength, health, and vitality.</p>';
		  break;
		case "yellow" :
		  echo '<p>Yellow is a color of happiness, wisdom, and imagination.</p>';
		  break;
		case "orange" :
		  echo '<p>Orange is a color of youth, strength, and fearlessness.</p>';
		  break;
		case "green" :
		  echo '<p>Green is a color of harmony, balance, and peace.</p>';
		  break;
		}
} else {
	echo "Some fields are missing!";
}

?>

For the sake of simplicity, we skipped data validation in our examples, but generally whenever working with user input it is important to validate the data, not only to prevent data corruption, but also to prevent security vulnerabilities. You should never trust user input and remember to always validate the data before using it inside your scripts. In the second part of this tutorial we’ll explain how to work with check-boxes and select elements, that allow the user to choose multiple values for each form element.

  • Share/Bookmark

Comments are currently disabled, but you can trackback from your own site.