An Example of Forms


One of the most common uses for PHP scripting in a web site is to process information users give to the site and produce an output based on that. Therefore, we will show a sample program that presents the user with a form, takes his name and address, and then prints out the information. Many of the programs we will write in later chapters will use this style of interaction extensively. This program is shown in Listings 7.1 and 7.2. (Each listing represents one file.)

Listing 7.1. newcustomer.html
 <html> <head>   <title>New Customer Information</title> </head> <body> <br/><br/> <p align='center'>     Please enter your full name and your user name: </p> <form action='/php/process_newcustomer.php' method='post'>   <table align='center' width='60%' border='0'>   <tr>     <td width='150'> Full Name:</td>     <td><input type='text' name='fullname' size='30'/></td>   </tr>   <tr>     <td width='150'> User Name:</td>     <td><input type='text' name='username' size='30'/></td>   </tr>   <tr>     <td colspan='2' align='center'>       <input type='submit' value='Submit'/>     </td>   </tr>   </table> </form> </body> </html> 

Listing 7.2. process_newcustomer.php
 <html> <head>   <title>Welcome !!!</title> </head> <body> <?php     $fullName = $_POST['fullname'];     $userName = $_POST['username']; ?>   <br/><br/>   <p align='center'>     Welcome new customer!  You have entered the following information:   </p>   <p align='center'>     Full Name: <b> <?php echo $fullName; ?> </b><br/>     User Name: <b> <?php echo $userName; ?> </b><br/>   </p> </body> </html> 

Take the two files and place them in the document area of your web site. (As long as they're both in the same place, the exact location doesn't matter.) Load newcustomer.html into your web browser from the directory in which you placed ithttp://hostname/myphpscripts/newcustomer.html. (Replace hostname with the name of your web server.) You will see something similar to Figure 7-1.

Figure 7-1. The new customer input form.


You can then enter some information and click on "Submit." You should see something akin to Figure 7-2.

Figure 7-2. The new customer information has been processed.


The new customer form is a simple HTML page with a <form> tag embedded in the page. Slots for a customer's full name and a user name are on this form. The key item is the action attribute on the <form> tag, which tells the web browser to pass the data from the form to a PHP script called process_newcustomer.php when the user clicks on the "Submit" button. You will see that the two input boxes on the form have name attributes specified for them: 'fullname' and 'username.'

The process_newcustomer.php script processes the information the user had previously put in to the form. This is in a global array called $_POST, and appropriate items in the array are conveniently labeled with the same names as the input boxes on the previous page's form. (You will have noticed that the $_POST array seems similar to the value of the method attribute on the <form> tag. This is not a coincidencewe will learn more about this in this chapter and in Chapter 13, "Web Applications and the Internet.")

After fetching the user's information, we print it out for him for confirmation. We have successfully completed our first form-based web program. Let the good times roll!




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net