Chapter 5 - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 5

Description:

when the second form is submitted, the data can be retrieved as you would for a text field ... user clicks the map, the mouse location is sent to the server. ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 18
Provided by: jackd64
Category:
Tags: chapter

less

Transcript and Presenter's Notes

Title: Chapter 5


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 toltform action"https//php.radford.edu/
    jcdavis/phpscript.php"
    method "post" gtltinput type"text"
    name"txt1" size"20"
    maxlength "30" /gtltinput type"submit"
    value"submit data" /gtlt/formgtif 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 tagltform name"fm1"
    action"https//php.radford.ed " method
    "post" gtform field examples ---ltpgtEnter
    first nameltinput type"text" size"20"
    maxlength"30" name"fn"
    /gtlt/pgtltpgtltinput type"checkbox" name"chk1"
    value"book1" checked"checked"
    /gtltinput type"radio" name"age"
    value"under20" /gtUnder 20ltbr /gtltinput
    type"radio" name"age" value"21-40"
    /gt21 - 40lt/pgt

5
form Fields (cont.)
  • ltinput type"submit" value"Submit Data"
    /gtltinput type"reset" value"Clear Form"
    /gtMenu's and text area's can also be used
    collect data on an html form.Processing
    data.in php codefrom a text boxtxt
    _POST"fn"from a check box, if a check box
    has been checked it's value will be included in
    the query stringchk1 _POST"chk1"chk1's
    value will be "book1"

6
Getting data from RB's
  • ltinput type"radio" name"age"
    value"lt20" /gtUnder 20ltbr /gtltinput type"radio"
    name"age" value"20-40" /gt20-40ltbr
    /gtltinput type"radio" name"age"
    value"over 40" /gtltbr /gt--------------------r
    eceiving codelt?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.ltinput type"hidden" name"hide1"
    value"under 20" /gtwhen 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. ltinput
    type"password" name"pass1" /gtit is read in
    php just like a text field.lt? 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 useltinput type"image" name"imap"
    src"imap.jpg" /gtwhen 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
    scriptltform enctype"multipart/form-data"
    action"phpfile.php" method"post"
    /gtUpload this file ltinput
    name"userfile" type"file"
    /gtltinput type"submit" value"Send File"
    /gtlt/formgt

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'user
    file''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 filelt?php handle
    fopen(_FILES'userfile''tmp_name',"
    r") while(!feof(handle)) text
    fgets(handle) echo text, "ltbr /gt"
    fclose (handle)?gt

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
    formltform action"phpbuttons.php"
    method"post" name "form1"gtltinput
    type"hidden" name"button" /gtltinput
    type"button" value"b1" onclick
    "button1()" /gtlt/formgt---------------
    javascripts next slide

14
Buttons Method 1
  • the following javascript would have to be
    included in the html form documentltscript
    language"JavaScript"gtlt!-- function
    button1() document.form1.Button.value
    "b1" form1.submit() function
    button2() document.form1.Button.value"b
    2" form1.submit() lt!-- one
    function per buttonlt/scriptgt--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
    inlt?php if (isset(_POST"Button"))
    echo _REQUEST"Button", "ltbr /gt"?gtwhy
    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.
Write a Comment
User Comments (0)
About PowerShow.com