Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
Name: <input type="text" name="name" /> <br /> Food: <input type="text" name="meal" value="pizza" /> <br /> <label>Meat? <input type="checkbox" name="meat" /></label> <br /> <input type="reset" />
value
attribute<fieldset>
,
<legend>
(6.2.8)
groups of input fields with optional caption (block)
<fieldset> <legend>Credit cards:</legend> <input type="radio" name="cc" value="visa" checked="checked" /> Visa <input type="radio" name="cc" value="mastercard" /> MasterCard <input type="radio" name="cc" value="amex" /> American Express </fieldset>
fieldset
groups related input fields, adds a border; legend
supplies a captionI changed the form's HTML code ... but when I refresh, the page doesn't update!
element[attribute="value"] { property : value; property : value; ... property : value; }
input[type="text"] { background-color: yellow; font-weight: bold; }
input
)<input type="text" name="username" /> Name <br /> <input type="text" name="sid" /> SID <br /> <input type="hidden" name="school" value="UW" /> <input type="hidden" name="year" value="2048" />
<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 />
[cc] => on, [startrek] => Jean-Luc Picard
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
" "
, "/"
, "="
, "&"
"Marty's cool!?"
→ "Marty%27s+cool%3F%21"
$_REQUEST
array automatically decodes themGET
vs. POST
requests
(6.3.3)
GET
: asks a server for a page or data
POST
: submits data to a web server and retrieves the server's response
POST
request is more appropriate than a GET
GET
requests embed their parameters in their URLs<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>
if ($_SERVER["REQUEST_METHOD"] == "GET") { # process a GET request ... } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { # process a POST request ... }
$_SERVER
array's "REQUEST_METHOD"
element<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>
input
tag with type
of file
enctype
attribute of the formpost
(an entire file can't be put into a URL!)enctype
(data encoding type) must be set to multipart/form-data
or else the file will not arrive at the serverArray | Description |
---|---|
$_REQUEST
|
parameters passed to any type of request |
$_GET ,
$_POST
|
parameters passed to GET and POST requests |
$_SERVER ,
$_ENV
|
information about the web server |
$_FILES
|
files uploaded with the web request |
$_SESSION ,
$_COOKIE
|
"cookies" used to identify the user (seen later) |
$blackbook = array(); $blackbook["marty"] = "206-685-2181"; $blackbook["stuart"] = "206-685-9138"; ... print "Marty's number is " . $blackbook["marty"] . ".\n";
"marty"
maps to value "206-685-2181"
print "Marty's number is {$blackbook['marty']}.\n";
$_FILES
, not $_REQUEST
$_FILES
is itself an associative array, containing:
name
: the local filename that the user uploadedtype
: the MIME type of data that was uploaded, such as image/jpeg
size
: file's size in bytestmp_name
: a filename where PHP has temporarily saved the uploaded file
<input type="file" name="avatar" />
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"
$username = $_REQUEST["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"; }
is_uploaded_file(filename)
TRUE
if the given filename was uploaded by the user
move_uploaded_file(from, to)
is_uploaded_file
, then do move_uploaded_file
include
(5.4.2)
include("filename");
include("header.php");