Creating Text Fields


Text fields are those controls into which the user can enter single-line text, and they're very common in web pages. To create an HTML text field in a form, use the <INPUT TYPE="TEXT"> element enclosed in an HTML <FORM> element.

In this case, we'll use two web documentsan HTML page that displays a text field, and a PHP script that will read what the user entered and will display that text in a new web page. In the HTML page, we're going to need an HTML form to send the text in the text field to a PHP script named phptext.php:

 <HTML>     <HEAD>         <TITLE>             Using Text Fields         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Text Fields             </H1>             <FORM METHOD="POST" ACTION="phptext.php">                 .                 .                 .             </FORM>         </CENTER>     </BODY> </HTML> 

In this example, we'll ask for the user's name. We'll call the text field "Name" using the text field's NAME attribute so that we can refer to it in PHP code in order to recover the data the user entered into the text field. You can see how that looks in Example 5-1, phptext.html.

Example 5-1. A text field, phptext.html
 <HTML>     <HEAD>         <TITLE>             Using Text Fields         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Text Fields             </H1>             <FORM METHOD="POST" ACTION="phptext.php">                 What's your name?                 <INPUT NAME="Name" TYPE="TEXT">                 <BR>                 <BR>                 <INPUT TYPE="SUBMIT" VALUE="Submit">             </FORM>         </CENTER>     </BODY> </HTML> 

That completes phptext.html; navigate to this web page (http://localhost/phptext.html if you're using a local server), as shown in Figure 5-1. You can see the prompt to the user here, the text field, and the Submit button.

Figure 5-1. Asking for the user's name.


To use this page, the user enters his or her name and then clicks the Submit button. The user's name is sent to our PHP script. And in that script, we're going to extract the user's name and echo it back in a new web page. See the next chunk for the details.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

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