home—lects—exams—hws
D2L—breeze (snow day)
lect03b-forms
html input forms, and php
chapter 3
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
- HTML tags for Input Fields
- Form Example
-
Remember:
<input name='aNodeName' /> uses the name attribute to
pass information in the action/request,
but <label for='aNodeId'>… uses id to
refer to the DOM node of the current tree.
(It makes good sense, but takes a moment of thought.)
- GET & POST methods
Q: But how is information communicated from a web form (pure html) to a php program?
A: When you click 'submit', the HTML makes a page-request to the page specified by the form's "action" attribute.
(Presumably it's a php page.)
The page-request incudes extra information about what html input fields had been selected, etc.,
as part of the http request.
The server gets that request and invokes the php file as normal, but it also
pre-initializes an array of values for the program — and it fills that array with
the extra information contained in the page-request.
- GET - form data is sent to the server appended to the server script URL
In the server script, data is retrieved via the super global associative array $_GET.
- POST - form data is embedded in the http request header
Server script - $_POST
When to use which?:
-
GET requests can be bookmarked;
-
GET requests can't be too long (not > 1000s of characters?; think text-submit boxes)
-
POST requests are more secure against casual looking over the shoulder
(but still vulnerable to packet sniffing etc.)
-
When refreshing a page created via POST,
browsers tend to respond:
“Refreshing will cause the page to re-submit; do you really want to do this?”
-
Possible guideline (?): If the page is changing a database's contents, use POST.
-
Many people suggest using POST as default.
-
Explore: How does a newspaper handle it? nytimes.
Discuss:
When might you choose to use post/get vs. could
just have a separate URL for each result,
e.g. different xkcd strips.
1
Note that everything the HTML designers bundled together
under the input tag do have
one thing in common:
it's not allowed to have a body.
I can see wanting to syntactically name which tags are forbidden
a body and which allow one, but that still doesn't explain why
each input-type isn't its own tag,
which would then make all input tags treated equally.
↩
home—lects—exams—hws
D2L—breeze (snow day)