Creating a Form to Ask a Question

Creating a Form to Ask a Question

It's very typical for PHP programs to be made of two or more separate documents. An ordinary HTML page contains a form, which the user fills out. When the user presses the submit button, the information in all the form elements is sent to a program specified by a special attribute of the form. This program processes the information from the form and returns a result, which looks to the user like an ordinary Web page. To illustrate, look at the whatsName.html page illustrated in Figure 2.7.

Click To expand
Figure 2.7: This is an ordinary HTML page containing a form.

The whatsName.html page does not contain any PHP at all. It's simply an HTML page with a form on it. When the user clicks on the Submit Query button, the page sends the value in the text area to a PHP program called hiUser.php. Figure 2.8 shows what happens when the hiUser.php program runs:

Click To expand
Figure 2.8: The resulting page uses the value from the original HTML form.

It's important to recognize that two different pages are involved in the transaction. In this section, you'll learn how to link an HTML page to a particular script, and how to write a script that expects certain form information.

Building an HTML Page with a Form

Forms are very useful when you want to get information from the user. To illustrate how this is done, look at the code for the whatsName.html file.

<html>
<head>
<title>What's your name?</title>
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "post"
      action = "hiUser.php">
Please type your name:
<input type = "text"
       name = "userName"
       value = "">
<br>
<input type = "submit">

</form>
</body>
</html>

There is only one element of this page that may not be familiar to you. Take a careful look at the form tag. It contains two new attributes. The method attribute indicates how the data will be sent to the browser. There are two primary methods, get and post. The post method is the most powerful and flexible, so it is the one I use most often in this book. However, you'll see some interesting ways to use the get method later in this chapter in the section called "Sending Data without a Form."

Setting the Action Attribute to a Script File

The other attribute of the form tag is the action attribute. This is used to determine the URL of a program that will interpret the form. This attribute is used to connect a Web page to a program to read the page and respond with another page. The URL can be an absolute reference (which begins with http:// and contains the entire domain name of the response program), or they can be relative references (meaning the program will be in the same directory as the original Web page).

The whatsName.html page contains a form with its action attribute set to hiUser.php. Whenever the user clicks on the submit button, the values of all the fields (there's only one in this case) will be packed up and sent to a program called hiUser.php, which is expected to be in the same directory as the original whatsName.html page.

Writing a Script to Retrieve the Data

The code for hiUser.php is specially built. The form that called the hiUser.php code is expected to have an element called userName. Take a look at the code for hiUser.php and you'll see what I mean.

IN THE REAL WORLD
Start example

Some PHP servers have turned off the ability to automatically create a variable from a form. You might be able to convince your server administrator to turn register_globals on in the PHP.INI file. If not, here's a workaround: If your form has a field called userName, add this code to the beginning of the program that needs the value of that field:

$userName = $_REQUEST["userName"];

Repeat this code for every variable you wish to pull from the original form.

For a complete explanation of this code, you'll need to skip ahead to Chapter 5, "Better Arrays and String Handling." In that chapter, you'll also find a routine for automatically extracting all the fields of a form even if you don't know the names of the fields.

End example
<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<h3>PHP program that receives a value from "whatsName"</h3>

<?

  print "<h3>Hi there, $userName!</h3>";

?>

</body>
</html>

Like many PHP pages, hiUser.php is mainly HTML. The only thing that's different is the one print statement. This statement incorporates the variable $userName. The puzzling thing is there is no other mention of the variable anywhere in the code.

When a user submits a form to a PHP program, the PHP processor automatically creates a variable with the same name as every form element on the original HTML page. Since the whatsName.html page has a form element called userName, any PHP program that whatsName.html activates will automatically have access to a variable called $userName. The value of that variable will be whatever the user has entered into the field before pressing the Submit button.