Presentation on theme: "1 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science."— Presentation transcript:
1 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology Radford University
2 HTML Form Fields in this chapter, we look at how to read data from form fields on html documents buttons checkboxes hidden fields image maps lists passwords radio buttons reset button select (Menus) submit button text areas text fields
3 Handling Client Data Review an html document may contain a form. A form tag includes an action attribute, that specifies the server side script to send the form data to if method is post, the data will be accessed via the $_POST array, if method is get then the data will be in $_GET array. the $_REQUEST array holds data from both post and get. These are superglobal arrays.
4 html Form form tag form field examples --- Enter first name: Under 20 21 - 40
5 form Fields (cont.) Menu's and text area's can also be used collect data on an html form. Processing data. in php code from a text box $txt = $_POST["fn"]; from a check box, if a check box has been checked it's value will be included in the query string $chk1 = $_POST["chk1"]; $chk1's value will be "book1"
6 Getting data from RB's Under 20 20-40 -------------------- receiving code
"name": "6 Getting data from RB s Under 20 20-40 -------------------- receiving code < php $age = $_POST[ age ]; What will $age value be if the first radio button is clicked",
"description": "6 Getting data from RB s Under 20 20-40 -------------------- receiving code < php $age = $_POST[ age ]; What will $age value be if the first radio button is clicked",
7 Hidden Fields let's you store hidden data in html documents say you had a multi-form application, so the user fills out one form and clicks on submit. the server application collects the data and then wants to have the user enter more info. on a new form. hidden fields can be used to insert this data into the second form, so it will return when the submit button on the second form is clicked. when the second form is submitted, the data can be retrieved as you would for a text field
8 Password Form Fields In PHP these are nearly the same as text boxes. However, the characters the user enters into the text field are not echoed to the screen. Rather an asterisk is echoed so the password can not be read by casual viewers of the screen. it is read in php just like a text field. $password = $_POST["pass1"]; Note -- the password is not necessarily encrypted in the query string
9 Image Maps PHP supports html image maps, which are clickable images full of hot spots, although you work with them differently in php. to create an image map you use when the user clicks the map, the mouse location is sent to the server. $xloc = $_POST["imap_x"]; when you know where the map was clicked you can use that info. to take different actions
10 Uploading Files HTML forms can be used to upload files. Assume you wish to upload a file named message.txt and make this text available to the php server script Upload this file:
11 Reading Uploaded Files There's another superglobal array $_FILES, it gives you access to the uploaded file $_FILES['userfile']['name'] original name of the file from user $_FILES['userfile']['type'] MIME type of the file for example -- "text/plain" or "image/gif") $_FILES['userfile']['size'] size of the uploaded file in bytes $_FILES['userfile']['tmp_name'] temporary filename of the file in which the uploaded file was stored on the server when a file is uploaded, it's stored as a temporary file on the server, and you access using - $_FILES['userfile']['tmp_name']
12 Reading Uploaded Files (cont) We'll see more about reading and writing files in chapter 6, but briefly here's the method used to open and read an uploaded file "; } fclose ($handle); ?>
15 Buttons Method 2 The second method is to build multiple forms on one html document. One form for each button. Each button could then be built using a submit button (there can only be one submit button on a form). There would be a hidden field in each form that contains the name of the submit button for that form One php server script can be specified in the action attribute of each form. The identity of the button can be read in the php server script by reading the hidden field. Each hidden field can be preset with the name of the submit button in that form. The problem with this approach is that if there is extra data to be included on the form, it has to be on all three forms. So, this method is rarely used.
16 Buttons Method 3 You can pass data back to the server script by using the value attribute of the submit button. You still must use three different forms, each with a submit button with the same name. We still have three forms, but the hidden field on each form is not needed. Each submit button would have a different value, the string displayed on the face of the button. In the php server script we would access the value as in: "; ?> why could you use both the $_POST and the $_REQUEST super global arrays?
17 Class Exercise Build a three part form - the first document is a form that has a text field that calls one php server script. This script puts the contents of the text field into another form and asks the user to indicate how many times they want to concatenate that entry together. It calls a second php script. The second server script performs the concatenation and outputs back to the client the original string entered and the concatenated string. This script should contain a function that is called multiple times to do the concatenation, it should utilize a static variable.