Php form input, Exams of Advanced Computer Programming

for programming

Typology: Exams

2014/2015

Uploaded on 09/16/2015

sumair_sajid
sumair_sajid 🇵🇰

1 document

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP Form Handling
Prof. Jim Whitehead
CMPS 183 – Spring 2006
May 3, 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Php form input and more Exams Advanced Computer Programming in PDF only on Docsity!

PHP Form Handling

Prof. Jim Whitehead

CMPS 183 – Spring 2006

May 3, 2006

Importance

  • A web application receives input from the

user via form input

  • Handling form input is the cornerstone of

a successful web application – everything

else builds on it

HTML Form Creation

• FORM

  • Encloses all input fields
  • Defines where and how to submit the form data
  • INPUT
  • Defines a specific input field
  • TEXTAREA
  • Creates a free-form text fill-in box
  • SELECT
  • Creates a menu
  • OPTION defines options within the menu

FORM

  • FORM attributes
    • action
      • URL of the resource that receives the filled-in form
      • This is the URL of your PHP code that receives the input
    • method
      • Choices are “get” or “post” – you should choose “post”
    • enctype
      • MIME type used to send results. By default is application/x- ww-form-urlencoded
      • Would use multipart/form-data if submitting a file (INPUT, type=file)

INPUT Control Types

  • text : single input line
  • password : single input line, with input characters obfuscated
  • checkbox : creates a check list
  • radio : creates a radio button list (checkbox, where inputs are mutually exclusive – only one input at a time)
  • button : push button
  • hidden : a hidden control. No input field is visible, but value is submitted as part of the form

INPUT control types

  • Special buttons
    • submit : the submit button. Causes input to be sent to the server for processing
    • reset : the reset button. Causes all input fields to be reset to their initial values
  • File upload
    • file : creates a file upload control

Receiving form input in PHP

  • Upon receiving a form submission, PHP automatically creates and populates two arrays with the form input data - Either: _POST[] or _GET[], depending on the FORM method type (post or get) - Also: HTTP_POST_VARS[] or HTTP_GET_VARS[] - Also: $name - Requires register_globals option, and is generally a bad idea due to cross-site attacks - Additionally, _REQUEST[] is also created
  • The array indicies are the names of the form variables (INPUT name=…)
  • The array value is the user entry data

Validating Data

  • All web applications must validate user

input data

  • Ensure the data is syntactically correct, and meets input constraints
  • Ideally want one PHP page to create

form, validate input, and handle correct

input

  • This is a little tricky

How to determine if first time

  • Can check if the $_POST[] array is empty
    • Will be empty first time through
    • if (empty($_POST)) { create initial form }
    • if (!empty($_POST)) { validate input }

All-in-one structure

  • Functions for input validation
  • Function(s) that creates form
    • Default values set from contents of _POST[]
  • HTML preamble
  • Create initial form
  • Validate input
  • Recreate form
  • Code to handle valid input
  • HTML end