Now we discusses what brings many people to PHPworking with HTML controls, such as text fields, checkboxes, radio buttons, listboxes, and others. This is where your web pages come alivethey start getting interactive as we begin reading data sent to us from the user, such as the user’s name, credit card number, and so on. They will be able to select pizza toppings, shoe sizes, and shipping datesand you’ll be able to handle that data back on the server, accessing it in your code. The HTML controls that we’re going to embed in web pages appear in Table
Table The HTML controls | |
Control | Description |
Buttons | Customizable buttons created with <BUTTON>. |
Checkboxes | A checkbox, created with the <INPUT TYPE=CHECKBOX> element. |
Hidden controls | Controls that contain text but do not appear in the web page. Created with the <INPUT TYPE=HIDDEN> element. |
Image maps | Clickable image maps created with <INPUT TYPE=IMAGE>. |
Lists | A multi-line list control, created with the <SELECT> element. |
Password | Creates a password control using <INPUT TYPE=PASSWORD">. |
Radio buttons | A radio button, created with the <INPUT TYPE=RADIO> element. |
Reset | A reset button created with <INPUT TYPE=RESET>. |
Select controls | Lists that let you select one or more items; uses <SELECT>, <OPTION>, and<OPTGROUP>. |
Submit | A submit button created with <INPUT TYPE=SUBMIT>. |
Text areas | A multi-line text control, created with the <TEXTAREA> element. |
Text fields | A one-line text control, created with the <INPUT TYPE=TEXT> element. |
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 toGET (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://your_host.com/your_folder/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 POSTmethod, 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 mean that they’re available to you without having to use the global keyword. Also, the $_REQUEST array holds data from both $_GET and $_POST.
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 NAMEattribute 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 1-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 1-1. You can see the prompt to the user here, the text field, and the Submit button.Figure 1-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.RETRIEVING DATA FROM TEXT FIELDS
In the previous chunk, we created a web page with an HTML text field in a form that will send the contents of the text field to phptext.php when the user clicks the Submit button. So how can you retrieve the data from that text field?We’ll embed the user’s name in a web page starting with the text "Your name is":<HTML> <HEAD> <TITLE> Using Text Fields </TITLE> </HEAD> <BODY> <CENTER> <H1> Retrieving Data From Text Fields </H1> Your name is . . . </CENTER> </BODY> </HTML>To retrieve data from the web page, we’ll use the $_REQUEST array, which holds the combined data of the $_POST and $_GET arrays, which means that you can use $_REQUEST with web pages that send you data using either the GET or POST methods. In the HTML page, phptext.html, we named the text field"Name" with the HTML NAME attribute, so to recover the value in that text field in the PHP script, you just use $_REQUEST["Name"], as shown in phptext.php, Example 1-2.Example 1-2. Reading data from a text field, phptext.php
<HTML> <HEAD> <TITLE> Using Text Fields </TITLE> </HEAD> <BODY> <CENTER> <H1> Retrieving Data From Text Fields </H1> Your name is <?php echo $_REQUEST["Name"]; ?> </CENTER> </BODY> </HTML>That’s all it takes; now we’re able to read the data the user entered into a text field. You can see the results in Figure 1-2, where we’ve gotten the user’s name and displayed it. Now we’re interacting with and accepting input from the user. Very cool.Figure 1-2. Reading data from a text field
That’s how it works in PHPyou recover data from HTML controls like this: $_REQUEST["ControlName"], which will handle both GET and POST requests. If you prefer, you can use the $_GET or $_POST arrays instead; for example, if you’ve used the POST method:<FORM METHOD="POST" ACTION="phptext.php"> What's your name? <INPUT NAME="Name" TYPE="TEXT"> <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM>Then you could use $_POST to recover data with:Your name is <?php echo $_POST["Name"]; ?>CREATING TEXT AREAS
Text areas act like multi-line text fields, and you create them with the HTML <TEXTAREA> element. You can see an example, phptextarea.html, in Example 1-3, where we’re asking the user to list his or her friends in a text area named "Friends". When the user clicks the Submit button here, that data will be sent to a PHP script, phptextarea.php, and we’ll echo that data in a new web page.Example 5-3. A text area, phptextarea.html
<HTML> <HEAD> <TITLE>Using Text Areas</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Text Areas</H1> <FORM METHOD="POST" ACTION="phptextarea.php"> Please list your best friends: <BR> <TEXTAREA NAME="Friends" COLS="50" ROWS="5"> 1. 2. 3. 4. </TEXTAREA> <BR> <BR> <INPUT TYPE="SUBMIT"> </FORM> </CENTER> <BODY> </HTML>This web page, textarea.html, appears in Figure 1-3, with some user data.
Figure1-3. Using a text area
To recover data from the text area in the PHP script phptextarea.php, you can use$_REQUEST["Friends"], as you see in Example 1-4.Example 5-4. A text area, phptextarea.php
<HTML> <HEAD> <TITLE> Using Text Areas </TITLE> </HEAD> <BODY> <CENTER> <H1> Retrieving Data From Text Areas </H1> Your best friends are: <?php echo $_REQUEST["Friends"]; ?> </CENTER> </BODY> </HTML>You can see the results in Figure 1-4, where we’ve read the text the user entered into the text area. No problem.Figure 1-4. Reading data from a text area.
CREATING CHECKBOXES
Another fundamental control is the checkbox control, which is created with the HTML <INPUT TYPE="CHECKBOX"> element. You can see an example in checkboxes.html, Example 1-5, where we’re asking the user if he or she wants some cash back.Example 1-5. Checkboxes, phpcheckboxes.html
<HTML> <HEAD> <TITLE>Using Checkboxes</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Checkboxes</H1> <FORM METHOD="POST" ACTION="checkboxes.php"> Do you want cash back? <INPUT NAME="Check1" TYPE="CHECKBOX" VALUE="Yes"> Yes <INPUT NAME="Check2" TYPE="CHECKBOX" VALUE="No"> No <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </CENTER> </BODY> </HTML>You can see the two checkboxes in a browser in Figure 1-5.Figure 1-5. Creating checkboxes.
You can determine which checkbox the user checked in your code using $_REQUEST["Check1"] and$_REQUEST["Check2"]. But what if the user only checked Check1 but not Check2? Asking for$_REQUEST["Check2"] will cause an error because it doesn’t exist. To check if it exists first, you can use the PHP isset function, which returns trUE if a variable has been set and FALSE otherwise. You can see what that looks like in the PHP script that reads the checkbox settings, checkboxes.php, Example 1-6.
Example 1-6. Reading data from checkboxes, phpcheckboxes.php
<HTML> <HEAD> <TITLE> Using Checkboxes </TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving Data From Checkboxes</H1> You checked <?php if (isset($_REQUEST["Check1"])) echo $_REQUEST["Check1"], "<BR>"; if (isset($_REQUEST["Check2"])) echo $_REQUEST["Check2"], "<BR>"; ?> </CENTER> </BODY> </HTML>As you can see in Figure 1-6, we have indeed been able to determine which checkbox the user checked. Nothing to it.Figure 1-6. Reading data from checkboxes.
CREATING RADIO BUTTONS
Users can check as many checkboxes as they want at once, but if you use radio buttons instead, the user can select only one of them at a time. That’s useful if you want to present a set of exclusive options, such as the current day of the week or whether the user is ordering a pizza, sandwich, or calzone.You group radio buttons together by giving two or more the same name, as you see in phpradio.html, Example 1-7.Example 1-7. Radio buttons, phpradio.html
<HTML> <HEAD> <TITLE>Using Radio Buttons</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Radio Buttons</H1> <FORM METHOD="POST" ACTION="radio.php"> Would you like cash back? <INPUT NAME="Radio1" TYPE="RADIO" VALUE="Yes"> Yes <INPUT NAME="Radio1" TYPE="RADIO" VALUE="No"> No <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </CENTER> </BODY> </HTML>And you can see phpradio.html at work in Figure 1-7.Figure 1-7. Creating radio buttons.
To recover the radio button that was selected in the radio button group, you use the name of the group with$_REQUEST, instead of having to work with each individual control as we did with checkboxes. You can see how this works in radio.php, Example 1-8.Example 5-8. Reading data from radio buttons, phpradio.php
<HTML> <HEAD> <TITLE>Using Radio Buttons</TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving Data From Radio Buttons</H1> <?php echo "You selected ", $_REQUEST["Radio1"]; ?> </CENTER> </BODY> </HTML>The results appear in Figure 1-8, where we have indeed determined which radio button in the group the user selected.Figure 1-8. Reading data from radio buttons.
What if you want multiple sets of radio buttons? No problem just gives each set of radio buttons its own name with the NAME attribute.CREATING LISTBOXES
Unlike the HTML controls we’ve already seen, HTML listboxes, created with <SELECT> controls, can support multiple selections. For example, take a look at phplistbox.html, Example 1-9, where we’re letting the user select his or her favorite fruits.Example 1-9. Listboxes, phplistbox.html
<HTML> <HEAD> <TITLE>Using Lists</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Lists</H1> <FORM METHOD="GET" ACTION="listbox.php"> Select your favorite fruit(s): <BR> <BR> <SELECT NAME="Food[]" MULTIPLE="TRUE"> <OPTION>Apple</OPTION> <OPTION>Orange</OPTION> <OPTION>Pear</OPTION> <OPTION>Pomegranate</OPTION> </SELECT> <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </CENTER> </BODY> </HTML>The user can select multiple listbox items, as you see in Figure 1-9.Figure 1-9. Using Listboxes.
We’ve named the listbox “Food” here, but because multiple selections are possible, we can’t just recover the selections using $_REQUEST["Food"]. Instead, we can refer to the first selection, if there is one, as$_REQUEST["Food"][0], the second, if there is one, as $_REQUEST["Food"][1], and so on. To catch them all, we’ll use a foreach loop as you see in listbox.php, Example 1-10.Example 1-10. Retrieving data from lists, listbox.php
<HTML> <HEAD> <TITLE>Using Lists</TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving Data From Lists</H1> You selected: <BR> <?php foreach($_REQUEST["Food"] as $fruit){ echo $fruit, "<BR>"; } ?> </CENTER> </BODY> </HTML>The results appear in Figure 1-10, where, as you can see, we’re retrieving the selections the user made in a multi-user listbox. Not bad.Figure 1-10. Reading data from listboxes.
CREATING HIDDEN CONTROLS
Another handy control type in PHP is the hidden control, which lets you store hidden text data in web pages. This is useful when you want to store data about a user and the user doesn’t allow cookies to be stored, for instance.Here’s an example, phphidden.html, Example 1-11, which contains the text “No worries.” in a hidden control named "Hidden".Example 5-11. Creating hidden data, phphidden.html
<HTML> <HEAD> <TITLE> Using Hidden Controls </TITLE> </HEAD> <BODY> <CENTER> <H1> Using Hidden Controls </H1> <FORM METHOD="POST" ACTION="phphidden.php"> Click the button to see the hidden data. <INPUT NAME="Hidden" TYPE="HIDDEN" VALUE="No worries."> <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Click Me"> </FORM> </CENTER> </BODY> </HTML>You can see this page, phphidden.html, in Figure 1-11. The data in the hidden control isn’t visible obviously but when you click the button, the PHP script phphidden.php will retrieve that data and display it.Figure 1-11. Using hidden controls.
You can retrieve the data from a hidden control as you’d expectby name, using $_GET or $_POST as appropriate, or by using $_REQUEST. You can see how that works in phphidden.php, Example 1-12, where we’re recovering the data in the hidden control as $_REQUEST["Hidden"].Example 5-12. Retrieving data from hidden controls, phphidden.php
<HTML> <HEAD> <TITLE> Retrieving Hidden Data </TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving Hidden Data</H1> The hidden data was: <BR> <?php echo $_REQUEST["Hidden"]; ?> </CENTER> </BODY> </HTML>The results appear in Figure 1-12, where we’ve been able to read the data from the hidden control and echo it in a web page.Figure 1-12. Reading data from hidden controls.
CREATING PASSWORD CONTROLS
Here’s another useful type of HTML control password controls. From a PHP point of view, these controls work just like text fields; but they’re different from the user’s point of view. The user sees only asterisks (*) each time he or she types a key instead of letters, so this control is handy for reading passwords and other sensitive data.You can see an example, phppassword.html, in Example1-13. This example asks the user what the password is by using a password control.Example 1-13. Using password controls, phppassword.html
<HTML> <HEAD> <TITLE> Using Password Controls </TITLE> </HEAD> <BODY> <CENTER> <H1> Using Password Controls </H1> <FORM METHOD="POST" ACTION="phppassword.php"> What's the password? <INPUT NAME="Password" TYPE="PASSWORD"> <BR> <BR> <INPUT TYPE="SUBMIT" VALUE=Submit> </FORM> </CENTER> </BODY> </HTML>You can see what this page looks like in Figure 1-13, where the user has already typed in a password (which in this case is “Open sesame”) into a password control named Password.Figure 1-13. Using a password control.
Although you can’t read the password when you type it in, the password is available to your PHP code. Use$_REQUEST["Password"], as you see in phppassword.php, Example 1-14.Example 5-14. Retrieving data from password controls, phppassword.php
<HTML> <HEAD> <TITLE> Retrieving Password Data </TITLE> </HEAD> <BODY> <CENTER> <H1> Retrieving Password Data </H1> You entered: <BR> <?php echo $_REQUEST["Password"]; ?> </CENTER> </BODY> </HTML>The result appears in Figure 1-14, where we’ve been successful in reading the data from the password control. As you’d expect, in a real application, you’d want to check the password against a password list and deny access unless it checks out.Figure 1-14. Reading data from a password control.
CREATING IMAGE MAPS
PHP also supports HTML-based image maps, which are clickable images full of hot spots, although you work with them a little differently in PHP.Here’s an example, phpimap.html, Example 1-15, which displays an image (imap.jpg) and lets the user click it. To create an image map this way, you use the HTML <INPUT TYPE="IMAGE"> element, setting theSRC attribute to the location of the image.Example 5-15. Creating an image map, phpimap.html
<HTML> <HEAD> <TITLE> Using Image Maps </TITLE> </HEAD> <BODY> <CENTER> <H1> Using Image Maps </H1> <FORM METHOD="POST" ACTION="phpimap.php"> Click the image: <BR> <INPUT NAME="imap" TYPE="IMAGE" SRC="imap.jpg"> </FORM> </CENTER> </BODY> </HTML>You can see what the image map looks like in Figure 1-15.Figure 1-15. Using an image map.
When the user clicks the map, the mouse location is sent to your PHP script. We’ve given the map the name "imap" using the HTML NAME attribute, and in other languages, you’d refer to the X and Y locations of the mouse as imap.x and imap.y. However, those aren’t legal PHP names, so in PHP, you use imap_xand imap_y, as shown in phpimap.php, Example 1-16.Example 5-16. Retrieving data from an image map, phpimap.php
<HTML> <HEAD> <TITLE>Retrieving Image Map Data</TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving Image Map Data</H1> <BR> You clicked at location ( <?php echo $_REQUEST["imap_x"], ", ", $_REQUEST["imap_y"]; ?> ). </CENTER> </BODY> </HTML>That’s all it takesnow your script can detect the mouse location of image map clicks, as shown in Figure 1-16. When you know where the map was clicked, you know what action the user wants you to take.Figure 1-16. Reading data from an image map.
UPLOADING FILES
HTML forms also let you upload files, and PHP is up to the task. For example, say that you want to upload a file named message.txtand make this text available to your code:No worries. PHP can handle this too.To upload files, you have to use a multipart form. How do you create one? You use the HTML <FORM>element’s ENCTYPE attribute:<FORM ENCTYPE="multipart/form-data" . . . </FORM>Now you’re free to specify the name of the script to send the file to, as usual:<FORM ENCTYPE="multipart/form-data" ACTION="phpfile.php" method="POST"> . . . </FORM>To actually do the uploading, use an HTML file control, created with an <INPUT TYPE="file"> element:<FORM ENCTYPE="multipart/form-data" ACTION="phpfile.php" method="POST"> Upload this file: <INPUT NAME="userfile" TYPE="FILE"> . . . </FORM>Note that in this case we’re naming the control "userfile", which will be how we refer to the uploaded filebut not using the $_GET, $_POST, or $_REQUEST arrays. Instead, you use a superglobal (i.e., available to all code) array named $_FILES, which we’ll do in the next chunk; our goal will be to display the contents of the uploaded file.All that’s left now is to add a Submit button, as shown in phpfile.html, Example 1-17.Example 5-17. A file upload control example, phpfile.html
<HTML> <HEAD> <TITLE> Uploading Files </TITLE> </HEAD> <BODY> <CENTER> <H1> Uploading Files </H1> <FORM ENCTYPE="multipart/form-data" ACTION="phpfile.php" method="POST"> Upload this file: <INPUT NAME="userfile" TYPE="FILE" /> <BR> <BR> <INPUT TYPE="SUBMIT" VALUE="Send File" /> </FORM> </CENTER> </BODY> </HTML>The file upload control appears in Figure 1-17; browse to the file you want to upload, which is message.txt in this example, and click the Send File button.Figure 5-17. An HTML upload control.
READING UPLOADED FILES
After you upload a file, you can access it using PHP, but it takes a little extra work. You use the superglobal array $_FILES to handle uploaded files; here are the specific elements you can use and what they mean. Note that the first index is the name of the file upload control, which is "userfile" in the previous chunk:$_FILES['userfile']['name']. The original name of the file on the user’s machine.$_FILES['userfile']['type']. The MIME type of the file. For example, this could be "image/gif" or "text/plain".$_FILES['userfile']['size']. The size of the uploaded file, in bytes.$_FILES['userfile']['tmp_name']. The temporary filename of the file in which the uploaded file was stored on the server.. The error code associated with this file upload.When a file has been uploaded, it is stored as a temporary file on the server, and you can access that file as $_FILES['userfile']['tmp_name']. We’re going to work with files in more part “Creating Web Forms and Validating User Input,” but we’ll get a preview of how to do that now. You start by using the fopen function to open the temporary file:<?php $handle = fopen($_FILES['userfile']['tmp_name'], "r"); . . . ?>You can loop over the lines of text in the file in a while loop that ends when we’ve reached the end of the file, which we can test with the feof function:<?php $handle = fopen($_FILES['userfile']['tmp_name'], "r"); while (!feof($handle)){ . . . } ?>To read lines of text from the file, use the fgets function like this, where we also display the text:<?php $handle = fopen($_FILES['userfile']['tmp_name'], "r"); while (!feof($handle)){ $text = fgets($handle); echo $text, "<BR>"; } ?>All that’s left is to close the temporary file with fclose, as shown in phpupload.php, Example 1-18.Example 5-18. Reading an uploaded file, phpupload.php
<HTML> <HEAD> <TITLE>Retrieving File Data</TITLE> </HEAD> <BODY> <CENTER> <H1>Retrieving File Data</H1> <BR> Here are the contents of the file: <BR> <?php $handle = fopen($_FILES['userfile']['tmp_name'], "r"); while (!feof($handle)){ $text = fgets($handle); echo $text, "<BR>"; } fclose($handle); ?> </CENTER> </BODY> </HTML>The results appear in Figure 1-18, where we’ve successfully uploaded the file. Very cool.Note: please create a .txt file for uploadFigure 1-18. Reading text from an uploaded file.
CREATING BUTTONS: TAKE 1
Another control you’ll see often in web pages is the HTML button. Buttons are different from other controls, however, because when clicked, they don’t stay clicked, which means that when the form is sent to your script, the button has long since popped up. So how do you determine which button the user clicked from PHP? One way is with a mix of JavaScript and a hidden control, as shown in phpbuttons.html, Example 1-19. When the user clicks a button, the JavaScript in this example stores the button name in a hidden HTML control named "Button" and then uses the submit function to post the results to a PHP script, phpbuttons.php.Example 1-19. Using buttons, phpbuttons.html
<HTML> <HEAD> <TITLE>Using Buttons</TITLE> </HEAD> <BODY> <H1>Using Buttons</H1> <FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="Button"> <INPUT TYPE="BUTTON" VALUE="Button 1" ONCLICK="button1()"> <INPUT TYPE="BUTTON" VALUE="Button 2" ONCLICK="button2()"> <INPUT TYPE="BUTTON" VALUE="Button 3" ONCLICK="button3()"> </FORM> <SCRIPT LANGUAGE="JavaScript"> <!-- function button1() { document.form1.Button.value = "button 1" form1.submit() } function button2() { document.form1.Button.value = "button 2" form1.submit() } function button3() { document.form1.Button.value = "button 3" form1.submit() } // --> </SCRIPT> </BODY> </HTML><HTML> <HEAD> <TITLE>Using Buttons</TITLE> </HEAD> <BODY> <H1>Using Buttons</H1> <FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="Button"> <INPUT TYPE="BUTTON" VALUE="Button 1" ONCLICK="button1()"> <INPUT TYPE="BUTTON" VALUE="Button 2" ONCLICK="button2()"> <INPUT TYPE="BUTTON" VALUE="Button 3" ONCLICK="button3()"> </FORM> <SCRIPT LANGUAGE="JavaScript"> <!-- function button1() { document.form1.Button.value = "button 1" form1.submit() } function button2() { document.form1.Button.value = "button 2" form1.submit() } function button3() { document.form1.Button.value = "button 3" form1.submit() } // --> </SCRIPT> </BODY> </HTML>You can see what this page looks like in a browser in Figure 1-19.
Just click a button, and then you can read the name of the clicked button in the hidden field "Button" in your PHP script, as shown in phpbuttons.php, Example 1-20.Example 1-20. Reading buttons, phpbuttons.php
<HTML> <HEAD> <TITLE>Using Buttons</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Buttons</H1> You clicked <?php if (isset($_REQUEST["Button"])) echo $_REQUEST["Button"], "<BR>"; ?> </CENTER> </BODY> </HTML>You can see the results in Figure 1-20, where we’ve determined which button was clicked.Figure 1-20. Determining which button was clicked.
CREATING BUTTONS: TAKE 2
In the previous chunk, we used a mixture of JavaScript and a hidden control to determine which button of a set of three a user clicked in a web page. However, you can do the same thing without any JavaScript if you use three Submit buttons as your buttons.To do that, you use three different HTML forms in your web page, each with its own Submit button. To the user, it looks as if you have three simple push buttons in your page, but when the user clicks one of the buttons, that form’s data is sent back to your PHP script:<FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="SUBMIT" VALUE="Button 1"> </FORM>To send some data back to the PHP script, we’ll add a hidden control to each of the three forms, containing the name of the clicked button, as shown in phpbuttons2.html, Example 1-21.Example 1-21. Using buttons: take 2, phpbuttons2.html
<HTML> <HEAD> <TITLE>Using Buttons: Take 2</TITLE> </HEAD> <BODY> <H1>Using Buttons: Take 2</H1> <FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 1"> <INPUT TYPE="SUBMIT" VALUE="Button 1"> </FORM> <FORM NAME="form2" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 2"> <INPUT TYPE="SUBMIT" VALUE="Button 2"> </FORM> <FORM NAME="form3" ACTION="phpbuttons.php" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 3"> <INPUT TYPE="SUBMIT" VALUE="Button 3"> </FORM> </SCRIPT> </BODY> </HTML>This page appears in Figure 1-21, where you can see the three Submit buttons masquerading as three simple push buttons.Figure 1-21. Determining which button was clicked, Take 2.
To read the name of the clicked button in phpbuttons2.php, Example 1-22, just extract the text from the hidden control, "Button".Example 1-22. Reading buttons: take 2, phpbuttons2.php
<HTML> <HEAD><TITLE>Using Buttons: Take 2</TITLE></HEAD> <BODY><CENTER> <H1>Using Buttons: Take 2</H1> You clicked <?php if (isset($_REQUEST["Button"])) echo $_REQUEST["Button"], "<BR>"; ?> </CENTER></BODY> </HTML><HTML> <HEAD><TITLE>Using Buttons: Take 2</TITLE></HEAD> <BODY><CENTER> <H1>Using Buttons: Take 2</H1> You clicked <?php if (isset($_REQUEST["Button"])) echo $_REQUEST["Button"], "<BR>"; ?> </CENTER></BODY> </HTML>The results appear in Figure 1-22, where we’ve been able to determine the clicked button.Figure 1-22. Indicating the clicked button, Take 2.
Here we’ve used hidden controls to store our data, but in fact there’s an even simpler technique where you don’t need them at all.CREATING BUTTONS: TAKE 3
We’ve seen two ways to handle push buttons in web pages, but there’s a third way; you can pass data back to a PHP script using the VALUE attribute of Submit buttons, which can be read in a PHP script. This technique avoids having to use a hidden control to send data back to your script.To do this, just give each Submit button the same name, "Button" in this next example, and assign the name of the button to the VALUE attribute in each of the three forms:<FORM NAME="form1" ACTION="phpbuttons3.php" METHOD="POST"> <INPUT TYPE="SUBMIT" NAME="Button" VALUE="button 1"> </FORM>You can see how this looks in a web page in phpbuttons3.html, Example 1-23.Example 1-23. Using buttons: take 3, phpbuttons3.html
<HTML> <HEAD> <TITLE>Using Buttons: Take 3</TITLE> </HEAD> <BODY> <H1>Using Buttons: Take 3</H1> <FORM NAME="form1" ACTION="phpbuttons3.php" METHOD="POST"> <INPUT TYPE="SUBMIT" NAME="Button" VALUE="button 1"> </FORM> <FORM NAME="form2" ACTION="phpbuttons3.php" METHOD="POST"> <INPUT TYPE="SUBMIT" NAME="Button" VALUE="button 2"> </FORM> <FORM NAME="form3" ACTION="phpbuttons3.php" METHOD="POST"> <INPUT TYPE="SUBMIT" NAME="Button" VALUE="button 3"> </FORM> </SCRIPT> </BODY> </HTML>As you see in Figure 1-23, all three buttons are visible, as before, in the browser.Figure 1-23. A set of buttons, Take 3.
In the PHP script, phpbuttons3.php, Example 5-24, all you need to do is the check value of the control named "Button"which this time is a Submit button, not a hidden field.Example 1-24. Reading buttons: take 3, phpbuttons3.php
<HTML> <HEAD> <TITLE>Using Buttons: Take 3</TITLE> </HEAD> <BODY> <CENTER> <H1>Using Buttons: Take 3</H1> You clicked <?php if (isset($_REQUEST["Button"])) echo $_REQUEST["Button"], "<BR>"; ?> </CENTER> </BODY> </HTML>As before, the PHP script can determine which button was clicked, as you see in Figure 5-24. Presto.Figure 1-24. Indicating the clicked button, Take 3.
You can determine which checkbox the user checked in your code using $_REQUEST["Check1"] and$_REQUEST["Check2"]. But what if the user only checked Check1 but not Check2? Asking for$_REQUEST["Check2"] will cause an error because it doesn’t exist. To check if it exists first, you can use the PHP isset function, which returns trUE if a variable has been set and FALSE otherwise. You can see what that looks like in the PHP script that reads the checkbox settings, checkboxes.php, Example 1-6.
Example 1-6. Reading data from checkboxes, phpcheckboxes.php
0 comments:
Post a Comment