Form Handling with PHP
One of you the ways you can provide interactivity to your website is by providing data entry forms where site visitors can send their orders, feedbacks, inquiries, subscriptions and the like. Forms are even more important in database-driven Web Applications. Can you imagine anything useful in a web application that doesn't provide forms. Forms are primarily the main interface where users get to interact with an application.
In tutorial, you will learn how to handle HTML forms in PHP. I am assuming that you are thoroughly familiar with creating HTML Forms. Can you do it by hand without the aid of and HTML Editor? If not, be nice to your self and fire-up that DreamWeaver Icon you placed on your desktop and get ready to rock and roll.
Let us begin the discussion by trying to solve the Multiplication Table Problem mentioned in the prior tutorial on PHP Control Structures. We'll allow the user to enter two numbers, one for the row and another for the column and then generate the Multiplication Table when he clicks on the Submit Button. Here is how the form looks like.
Listing 1.1.a: entry-form.php
-
<html>
-
<head>
-
<title>Data Entry Form</title>
-
</head>
-
<body>
-
<form action="multiplication-table.php">
-
<b>This will create an R x C Multiplication Table</b> <br />
-
-
Row (R): <input name="row" /> <br />
-
Column (C) : <input name="col" /> <br />
-
<input name="submit_button" type="submit"
-
value="Create Multiplication Table" /> <br />
-
</form>
-
</body>
-
</html>
The code above simply displays a data entry form with two input fields and a button that submits the form back to the server calling the script (multiplication-table.php) indicated in the ACTION attribute of the FORM tag.
Here is the code for the second script:
Listing 1.2.a: multiplication-table.php
-
<html>
-
<head>
-
<title>Multiplication Table</title>
-
</head>
-
<body>
-
<? php
-
$row = $_GET['row'];
-
$col = $_GET['col'];
-
-
// see to it that both are non-zero
-
print "$row x $col Multiplication Table <br />";
-
print '<table border="0" cellspacing="2">';
-
for ($r = 1; $r <= $row; ++$r) {
-
print '<tr>';
-
for ($c = 1; $c <= $col; ++$c){
-
$p = $r * $c;
-
print "<td>$p</td>";
-
}
-
print '</tr>';
-
}
-
print '</table>';
-
}
-
?>
-
</table></body>
-
</html>
For purposes of demonstration, let us assume for the moment that the user is so understanding he will provide positive integers on the input fields. When the form is submitted, the second script handles the entries and displays the Multiplication accordingly. Try if for yourself.
To capture the values of the input fields, we simple use the $_GET[] array and have the names of the fields as the indexes -- $_GET['row'] and $_GET['col']. This works because the form takes on the default get method. Remember the GET attribute of the FORM tag? It can take two valid values: "post" and "get" and it defaults to "get".
More on $_GET
Go back again to the entry form and take a look at the URL.
Enter some positive values.
Submit the form.
What happens to the URL?
It forms a Query String indicating the name of the form fields and their values. This is what happens when you use the "get" method on the form.
Good or bad? You decide.
What if the user is submitting some sensitive data like password and user name? Would you want to display it on the address bar of the browser for the rest of the people to see? What if you are asking for detailed user feedback that and could take a couple of paragraphs to compose? The Address Bar as we know can accommodate only a limited amount of characters and truncate the rest.
Enter $_POST
Certainly when using a form, it is ideal to use the post method to avoid the problems above. Below is the modified code for both the entry form and the action script this time using the post method.
Listing 1.1.b: entry-form.php
-
<html>
-
<head>
-
<title>Data Entry Form</title>
-
</head>
-
<body>
-
<form method="post" action="multiplication-table.php">
-
<b>This will create an R x C Multiplication Table</b> <br />
-
-
Row (R): <input name="row" /> <br />
-
Column (C) : <input name="col" /> <br />
-
<input name="submit_button" type="submit"
-
value="Create Multiplication Table" /> <br />
-
</form>
-
</body>
-
</html>
Listing 1.2.b: multiplication-table.php
-
<html>
-
<head>
-
<title>Multiplication Table</title>
-
</head>
-
<body>
-
<? php
-
$row = $_POST['row'];
-
$col = $_POST['col'];
-
-
// see to it that both are non-zero
-
print "$row x $col Multiplication Table <br />";
-
print '<table border="0" cellspacing="2">';
-
for ($r = 1; $r <= $row; ++$r) {
-
print '<tr>';
-
for ($c = 1; $c <= $col; ++$c){
-
$p = $r * $c;
-
print "<td>$p</td>";
-
}
-
print '</tr>';
-
}
-
print '</table>';
-
}
-
?>
-
</table></body>
-
</html>
About $_REQUEST
There is one additional variable that you need to be aware of when handling forms in PHP. Regardless of the method you use (whether get or post) in submitting the form, you can always capture the data by using the the $_REQUEST array. I have no clear idea why the developer of PHP ever came up with this variable. But, personally I would discourage you from using this variable. With $_REQUEST, it's hard to see where your variable is coming from by just looking at the code.
No Action
"Nothing happens until there is action." That is an old saying that the great Jose Rizal probably did not say. That saying is not true in HTML. When you remove the "action" attribute of the FORM and submits it, the form calls the script where it is contained. Essentially, it goes back to the server and calls itself. In which case, your action code should be contained where the form is.
Have Some Fun
We have only touched two kinds of form fields: the textbox and the submit button. There are more. Experiment on them. Develop a form where you have all kinds of HTML Form elements in it. Checkbox, radio buttons, drop down list, text area, reset button, etc. Use PHP to handle them. You will find it's really easy. Make it a habit to use post for the form method and $_POST to capture the values of the fields.
Got questions? Please send me your feedback.
This series of articles is intended as a
Self-Paced Introductory tutorial on PHP.
Sir,
I really appreciate that you informed us about how to deal with tables in PHP.
Thank you very much
Best regars,
Cesas