Docsity

Form Processing with PHP: Creating XHTML Forms, GET vs. POST, and Data Processing, Slides of Business Administration

Toggle sidebar
bg1
1
FORM PROCESSING WITH
PHP
Processing Form Data
Common client-side scripting task. Very easy in PHP.
Often involves using collected data in producing
fh ( h i) i i ih
f
urt
h
er output
(
searc
h
eng
i
ne
)
or
i
nteract
i
ng w
i
t
h
a database (store customer info).
XHTML form
collects
information
Database or other
PHP script
XHTML output of
results
Database
or
other
storage
Client/Server dividing line
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Form Processing with PHP: Creating XHTML Forms, GET vs. POST, and Data Processing and more Slides Business Administration in PDF only on Docsity!

1

FORM PROCESSING WITH

PHP

Processing Form Data

Common client-side scripting task. Very easy in PHP.

Often involves using collected data in producing

ffurther output (search engine) or interacting with h ( h i ) i i i h

a database (store customer info).

XHTML form collects information (^) Database or other PHP script

XHTML output of results

Database or other storage

Client/Server dividing line

2

Creating an XHTML Form

Key elements:

Input fields must be contained inside a form tag. All input fields must have a name. Names cannot have spaces in them. Fields should be named well for clear identification. Form action should be URL to PHP processing script. Appropriate form transmission method selected:pp p

GET vs. POST

GET:

Name/value pairs appended in clear text to the URL of the receiving page/script.g p g / p Each name/value pair separated by '&'. Value data automatically URL encoded. Names are taken from the form field names.

GET URLs can be saved, bookmarked, etc. and used to recall the script with the same data. GET strings provide 'transparency' that may/may not be desired. Long GET strings may be problematic.

4

Processing received GET or POST data

Data stored by web server in superglobal array

$_GET or $_POST, using field name as index.

$_GET and $_POST only available to receiving script

(not persistent).

Form field data passing specifics

Text fields, password fields, and textareas that make

up a form will always have their name (and value if

field is completed) passed in a formfield is completed) passed in a form.

Radio buttons and checkboxes will only have their

name passed (with the appropriate value) if the

button or checkbox has been set.

5

Conditional Statements

PHP supports traditional if construction with elseif

being a valid keyword.

if (condition)if (condition) { // conditional code here }

if (condition) { // conditional code here } else { // conditional code here }

7

Logical Operators

Operator Meaning Example ! Not !$a &&, and And $a && $b ||, or Or $a || $b xor exclusive or $a xor $b


Helpful form validation functions

Functions exist for testing data types:

is_numeric($x) , etc., but use can be

challengingchallenging.

isset($var) —does $var exist?

empty($var) —returns false unless $var contains

an empty string, 0, "0", NULL, or FALSE.

"; echo empty($testvar); ?>

8

What kind of validation is needed here?

Name:

Age:

Gender:

String manipulation functions useful with forms

trim($s) returns string $s with whitespace from beginning and end of a string removed. ltrimltrim($s)($s) returns string $s with leading white spacereturns string $s with leading white space removed. chop($s) returns string $s with trailing white space removed. strtoupper($s) —returns uppercase of string $s.

strtolowerstrtolower($s)($s) —returns lowercase of string $s. returns lowercase of string $s. ucfirst($s) —returns capitalized first character of string $s, if alphabetic. ucwords($s) —returns capitalized first character of every word in string $s, if alphabetic.

10

Client-side Form Validation

JavaScript, etc., permits client-side form validation,

however you should never rely on it to work.

BBrowser may have scripting turned off. h i i d ff User may save and edit page with client-side scripting edited.

Never trust any form data! Check to make sure it

fits the profile for valid data.fits the profile for valid data.