Handling User Data with Web Forms


To read data from HTML controls using PHP, you need to place those controls in HTML forms in your Web pages, using the HTML <FORM> element. Here are the important attributes of this element:

  • ACTION. This attribute provides the URL that will handle the form data. Note that you can omit this attribute, in which case its default is the URL of the current document.

  • METHOD. Specifies the method or protocol for sending data to the target action URL. If you set it to GET (the default), this method sends all form name/value pair information in a URL that looks like: URL?name=value&name=value&name=value. If you use the POST method, the contents of the form are encoded as with the GET method, but they are sent in hidden environment variables.

  • TARGET. Indicates a named frame for the browser to display the form results in.

For example, say that you wanted to read data that the user entered into the controls in a web page using a PHP script named phpreader.php in the same directory as phpreader.html. In that case, you could set the form's ACTION attribute to "phpreader.php" as here (if phpreader.php were not in the same directory, you'd have to give its URL, either relative to the current page or absolutely, such as http://some_isp.com/steve/phpreader.php):

 <HTML>     <HEAD>         <TITLE>             An HTML Form         </TITLE>     </HEAD>     <BODY>         <H1>             Using HTML Forms         </H1>         <FORM METHOD="POST" ACTION="phpreader.php">            .            .            .         </FORM>     </BODY> </HTML> 

Now you can stock your HTML form with controls such as text fields, radio buttons, and so on, and when the user puts data into those controls, all that data will be sent back to phpreader.php when the user clicks the Submit button. Forms like this one come standard with a Submit button, and you can add one to the form like this:

 <HTML><HEAD><TITLE>An HTML Form</TITLE></HEAD>     <BODY><H1>Using HTML Forms</H1>         <FORM METHOD="GET" ACTION="phpreader.php">            .            .            .             <INPUT TYPE="SUBMIT" VALUE="Submit">         </FORM>     </BODY> </HTML> 

This displays the clickable Submit button that you see in web pages. Note that the caption of this button doesn't have to be "Submit"; you can set it to whatever you want, using the VALUE attribute. Here's how to create a Submit button with the caption "Sign me up!"

 <INPUT TYPE="SUBMIT" VALUE="Sign me up!"> 

Besides Submit buttons, you can also display Reset buttons, which, when clicked, will reset the data in a form's controls back to their default (usually blank) values. Here's what a Reset button would look likenote that you can use any caption here, as with Submit buttons:

 <FORM METHOD="GET" ACTION="phpreader.php">             .             .             .     <INPUT TYPE="SUBMIT" VALUE="Submit">     <INPUT TYPE="RESET" VALUE="Reset"> </FORM> 

So how do you actually access the data that's sent to you in your PHP scripts? If you've used the POST method, you can find that data in the $_POST array, as we're going to see in the next chunk on retrieving data from text fields. If you've used the GET method, you use the $_GET array. These arrays are superglobal arrays, which means that they're available to you without having to use the global keyword. Also, the $_REQUEST array holds data from both $_GET and $_POST.



    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