1 / 17

Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner

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. HTML Form Fields. in this chapter, we look at how to read data from form fields on html documents buttons

desirae
Download Presentation

Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 – Handling HTML Controls in Web Pagesspring into PHP 5by Steven Holzner Slides were developed by Jack DavisCollege of Information Scienceand TechnologyRadford 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 • Reviewan 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<form action="https://php.radford.edu/ ~jcdavis/phpscript.php" method = "post" ><input type="text" name="txt1" size="20" maxlength = "30" /><input type="submit" value="submit data" /></form>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 name="fm1" action="https://php.radford.ed …" method = "post" >form field examples ---<p>Enter first name:<input type="text" size="20" maxlength="30" name="fn" /></p><p><input type="checkbox" name="chk1" value="book1" checked="checked" /><input type="radio" name="age" value="under20" />Under 20<br /><input type="radio" name="age" value="21-40" />21 - 40</p>

  5. form Fields (cont.) • <input type="submit" value="Submit Data" /><input type="reset" value="Clear Form" />Menu's and text area's can also be used collect data on an html form.Processing data.in php codefrom 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 • <input type="radio" name="age" value="<20" />Under 20<br /><input type="radio" name="age" value="20-40" />20-40<br /><input type="radio" name="age" value="over 40" /><br />--------------------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 documentssay 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.<input type="hidden" name="hide1" value="under 20" />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. <input type="password" name="pass1" />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<input type="image" name="imap" src="imap.jpg" />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<form enctype="multipart/form-data" action="phpfile.php" method="post" />Upload this file: <input name="userfile" type="file" /><input type="submit" value="Send File" /></form>

  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 serverwhen a file is uploaded, it's stored as a temporary file on the server, and you accessusing - $_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<?php $handle = fopen($_FILES['userfile']['tmp_name'],"r"); while(!feof($handle)) { $text = fgets($handle); echo $text, "<br />"; } fclose ($handle);?>

  13. Form Buttons, Method 1 • You may want have multiple buttons on an html form, the question how can you tell which button(s) was (were) clicked by the client • First method, when a button is clicked on a form put a description in a hidden field, then read that field in the php server script. A javascript will have to be included in the html form<form action="phpbuttons.php" method="post" name = "form1"><input type="hidden" name="button" /><input type="button" value="b1" onclick = "button1()" />…</form>--------------- javascripts next slide

  14. Buttons Method 1 • the following javascript would have to be included in the html form document<script language="JavaScript"><!-- function button1() { document.form1.Button.value = "b1"; form1.submit(); } function button2() { document.form1.Button.value="b2"; form1.submit(); }…<!-- one function per button</script>--the receiving script then reads the hidden field to determine which button was clicked

  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 formOne 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:<?php if (isset($_POST["Button"])) echo $_REQUEST["Button"], "<br />";?>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.

More Related