RU beehive logo ITEC dept promo banner
ITEC 325
2019fall
ibarland

html input forms, and php

a sample/demo form.

2019-Feb.14: We covered the first video below in class (and more) on Feb.12;
For Feb.14: watch the 2nd half of video: form0 checkboxes (array_key_exists, etc) (14m38s) (start at 8min); then finish by watching video: html input tags (minor details): radio buttons, select tags; boolean attributes; label tags; id vs name attributes (23m16s).

Forms and form-handlers:
A form is an html page where users may enter information (e.g. their favorite color, and whether or not they feel happy). Then, “submitting the form” just means requesting some other URL, but including the users's information along with that request.

video: form basics: passing key/values (12m36s)

Slightly more detail: When a user clicks submit, the browser takes the URL specified in the form tag — we call that URL the “form-handler”. As it requests the form-handler, it includes the form’s info along with the request, as key/value pairs (e.g. "fave-color" => "orange" and "isHappy" => "a-yep"). The form-handler (which might be a php program) can use this information to craft a response (html). Here is a small example.

The main thing to note is how the key-value pairs are specified. the form has input tags, with an attribute “name”. That name is what is used as the key, in the key-value pair! The value might come from the user (in the case of a input type='text' tag), or might be taken from the tag's value attribute (in the case of the checkbox shown).

Of secondary interest is the form tag itself. It's important attribute is action: When submitting, the browser merely requests the URL specified by action, and it also bundles along the key/value pairs specified by the form's inputs. If method='post', then the key/value pairs are instead passed along inside the http header (as already demo’d); if the method='get', the key/value pairs are attached at the end of the URL (you can check out form0-get.php).

We've already discussed that when php.radford.edu gets a request for a URL ending in .php, it runs that file as a program (returning anything that program printed out). But right before it runs the program:

  1. The server starts a php-process, in the OS.
  2. In that process’s memory-space, the server creates an array named “$_POST” and populates it with any key/value pairs that were in the http request packet.
  3. In that process’s memory-space, the server creates another array, named “$_GET”, and populates it with any key/value pairs at the end of the URL (following a “?” in the URL)
  4. Finally, it runs the requests php program, starting from line 1. That's why our PHP program has a arrays $_POST and $_GET to work with.
(Btw, there a few other arrays that are also pre-initialized for you; look up “PHP superglobals” if interested.)

label tags

You can use a label tag, to connect some text (anywhere on page) with (sending focus to) a particular input: See the example form below. Either enclose the text and the input inside the same label, or use label's for attribute.

Gotcha: While <input name='aNodeName'/> uses the name attribute to pass information to the action/request, the html <label for='aNodeId'>… uses id to refer to the DOM node of the current tree. (This actually makes good sense, but takes a moment of thought.)

Topics coming up: form-handlers and how to test them; grouping multiple inputs into an array; POST vs GET; sanitizing form info (against html-injection).


takeaways

video from distance lecture (breeze), 2017-feb-07 (1h42m), REVIEWING this info

1 Okay, in HTML5 it's allowed to not explicitly state a value at all, for a boolean-attribute. But for this class, we'll also follow XHTML's rule that every value must have an explicit value. Hence, checked='checked'.      
2 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.      
3 And later, when we talk about sticky-forms, I'll suggest “customer-info-edit.html” which requires either …-form or …-handle.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.