Web Programming Step by Step, 2nd Edition
Lecture 10: Submitting Data (POST)
Reading: 6.3 - 6.5
Except where otherwise noted, the contents of this document are
Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst.
All rights reserved.
Any redistribution, reproduction, transmission, or storage of part
or all of the contents in any form is prohibited without the author's
expressed written permission.
6.3: Submitting Data
-
6.1: Form Basics
-
6.2: Form Controls
-
6.3: Submitting Data
-
6.4: Processing Form Data in PHP
Problems with submitting data
<label><input type="radio" name="cc" /> Visa</label>
<label><input type="radio" name="cc" /> MasterCard</label> <br />
Favorite Star Trek captain:
<select name="startrek">
<option>James T. Kirk</option>
<option>Jean-Luc Picard</option>
</select> <br />
- this form submits to our handy params.php tester page
- the form may look correct, but when you submit it...
[cc] => on, [startrek] => Jean-Luc Picard
The value
attribute
<label><input type="radio" name="cc" value="visa" /> Visa</label>
<label><input type="radio" name="cc" value="mastercard" /> MasterCard</label> <br />
Favorite Star Trek captain:
<select name="startrek">
<option value="kirk">James T. Kirk</option>
<option value="picard">Jean-Luc Picard</option>
</select> <br />
value
attribute sets what will be submitted if a control is selected
[cc] => visa, [startrek] => picard
URL-encoding
- certain characters are not allowed in URL query parameters:
- examples:
" "
, "/"
, "="
, "&"
- when passing a parameter, it is URL-encoded
()
"Marty's cool!?"
→ "Marty%27s+cool%3F%21"
- you don't usually need to worry about this:
- the browser automatically encodes parameters before sending them
- the PHP
$_GET
and $_POST
arrays automatically decode them
-
... but occasionally the encoded version does pop up (e.g. in Firebug)
Submitting data to a web server
- though browsers mostly retrieve data, sometimes you want to submit data to a server
- Hotmail: Send a message
- Flickr: Upload a photo
- Google Calendar: Create an appointment
- the data is sent in HTTP requests to the server
- with HTML forms
- with Ajax (seen later)
- the data is placed into the request as parameters
HTTP GET
vs. POST
requests
-
GET
: asks a server for a page or data
- if the request has parameters, they are sent in the URL as a query string
-
POST
: submits data to a web server and retrieves the server's response
- if the request has parameters, they are embedded in the request's HTTP packet, not the URL
-
For submitting data to be saved,
POST
is more appropriate than GET
GET
requests embed their parameters in their URLs
- URLs are limited in length (~ 1024 characters)
- URLs cannot contain special characters without encoding
- private data in a URL can be seen or modified by users
Form POST example
<form action="http://foo.com/app.php" method="post">
<div>
Name: <input type="text" name="name" /> <br />
Food: <input type="text" name="meal" /> <br />
<label>Meat? <input type="checkbox" name="meat" /></label> <br />
<input type="submit" />
<div>
</form>
GET or POST?
if ($_SERVER["REQUEST_METHOD"] == "GET") {
...
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
...
}
- some PHP pages process both GET and POST requests
- to find out which kind of request we are currently processing,
look at the global $_SERVER
array's "REQUEST_METHOD"
element
6.4: Processing Form Data in PHP
-
6.1: Form Basics
-
6.2: Form Controls
-
6.3: Submitting Data
-
6.4: Processing Form Data in PHP
"Superglobal" arrays
-
PHP superglobal arrays contain information about the current request, server, etc.:
-
These are special kinds of arrays called associative arrays.
Associative arrays
$blackbook = array();
$blackbook["marty"] = "206-685-2181";
$blackbook["stuart"] = "206-685-9138";
...
print "Marty's number is " . $blackbook["marty"] . ".\n";
Uploading files
<form action="http://webster.cs.washington.edu/params.php"
method="post" enctype="multipart/form-data">
Upload an image as your avatar:
<input type="file" name="avatar" />
<input type="submit" />
</form>
- add a file upload to your form as an
input
tag with type
of file
- must also set the
enctype
attribute of the form
- it makes sense that the form's request method must be
post
(an entire file can't be put into a URL!)
- form's
enctype
(data encoding type) must be set to multipart/form-data
or else the file will not arrive at the server
Processing an uploaded file in PHP
-
uploaded files are placed into global array
$_FILES
, not $_POST
- each element of
$_FILES
is itself an associative array, containing:
name
: the local filename that the user uploaded
type
: the MIME type of data that was uploaded, such as image/jpeg
size
: file's size in bytes
tmp_name
: a filename where PHP has temporarily saved the uploaded file
- to permanently store the file, move it from this location into some other file
Uploading details
<input type="file" name="avatar" />
- example: if you upload
borat.jpg
as a parameter named avatar
,
$_FILES["avatar"]["name"]
will be "borat.jpg"
$_FILES["avatar"]["type"]
will be "image/jpeg"
$_FILES["avatar"]["tmp_name"]
will be something like "/var/tmp/phpZtR4TI"
Processing uploaded file, example
$username = $_POST["username"];
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
move_uploaded_file($_FILES["avatar"]["tmp_name"], "$username/avatar.jpg");
print "Saved uploaded file as $username/avatar.jpg\n";
} else {
print "Error: required file not uploaded";
}
- functions for dealing with uploaded files:
-
is_uploaded_file(filename)
returns TRUE
if the given filename was uploaded by the user
-
move_uploaded_file(from, to)
moves from a temporary file location to a more permanent file
- proper idiom: check
is_uploaded_file
, then do move_uploaded_file
Including files: include
include("filename");
include("header.html");
include("shared-code.php");
- inserts the entire contents of the given file into the PHP script's output page
- encourages modularity
- useful for defining reused functions needed by multiple pages
More about associative arrays
-
6.1: Form Basics
-
6.2: Form Controls
-
6.3: Submitting Data
-
6.4: Processing Form Data in PHP
-
More about associative arrays
Creating an associative array
$name = array();
$name["key"] = value;
...
$name["key"] = value;
$name = array(key => value, ..., key => value);
$blackbook = array("marty" => "206-685-2181",
"stuart" => "206-685-9138",
"jenny" => "206-867-5309");
- can be declared either initially empty, or with a set of predeclared key/value pairs
Printing an associative array
print_r($blackbook);
Array
(
[jenny] => 206-867-5309
[stuart] => 206-685-9138
[marty] => 206-685-2181
)
print_r
function displays all keys/values in the array
var_dump
function is much like print_r
but prints more info
- unlike
print
, these functions require parentheses
if (isset($blackbook["marty"])) {
print "Marty's phone number is {$blackbook['marty']}\n";
} else {
print "No phone number found for Marty Stepp.\n";
}
foreach
loop and associative arrays
foreach ($blackbook as $key => $value) {
print "$key's phone number is $value\n";
}
jenny's phone number is 206-867-5309
stuart's phone number is 206-685-9138
marty's phone number is 206-685-2181
- both the key and the value are given a variable name
- the elements will be processed in the order they were added to the array