Thursday 14 May 2009

Figure 6-1. Displaying and processing a simple form






The response to the first request is some HTML for a form. Figure shows what the browser displays when it receives that response.
Figure 6-2. A simple form



Figure 6-2. A simple form



The response to the second request is the result of processing the submitted form data. Figure shows the output when the form is submitted with Susannah typed in the text box.

AND MORE

Join Free Now : PHP Tutorial

Making Web Forms

Form processing is an essential component of almost any web application. Forms are how users communicate with your server: signing up for a new account, searching a forum for all the posts about a particular subject, retrieving a lost password, finding a nearby restaurant or shoemaker, or buying a book.
Using a form in a PHP program is a two-step activity. Step one is to display the form. This involves constructing HTML that has tags for the appropriate user-interface elements in it, such as text boxes, checkboxes, and buttons. If you're not familiar with the HTML required to create forms, the "Forms" chapter in HTML & XHTML: The Definitive Guide, by Chuck Musciano and Bill Kennedy (O'Reilly) is a good place to start.
When a user sees a page with a form in it, she inputs the information into the form and then clicks a button or hits Enter to send the form information back to your server. Processing that submitted form information is step two of the operation.
Example 6-1 is a page that says "Hello" to a user. If a name is submitted, then the page displays a greeting. If a name is not submitted, then the page displays a form with which a user can submit her name.
Example 6-1. Saying "Hello"if (array_key_exists('my_name',$_POST)) {
print "Hello, ". $_POST['my_name'];
} else {
print<<<_HTML_
_HTML_;
}
Remember the client and server communication picture from Chapter 1? Figure 6-1 shows the client and server communication necessary to display and process the form in Example 6-1. The first request and response pair causes the browser to display the form. In the second request and response pair, the server processes the submitted form data and the browser displays the results.
Figure 6-1. Displaying and processing a simple form

Join Free Now : PHP Tutorial