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:
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. |