Web Programming Step by Step

Lecture 9
HTML Forms

Reading: 6.1 - 6.2, 6.4

Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.

Valid XHTML 1.1 Valid CSS!

6.1: Form Basics

Web data

Query strings and parameters (6.1.1)


HTML forms

HTML form

HTML form: <form> (6.1.2)

<form action="destination URL">
	form controls

Form example

<form action="http://www.google.com/search">
		Let's search Google:
		<input name="q" />
		<input type="submit" />

6.2: Form Controls

Form controls: <input>

<!-- 'q' happens to be the name of Google's required parameter -->
<input type="text" name="q" value="Colbert Report" />
<input type="submit" value="Booyah!" />

Text fields: <input> (6.2.1)

<input type="text" size="10" maxlength="8" /> NetID <br />
<input type="password" size="16" /> Password
<input type="submit" value="Log In" />

Text boxes: <textarea> (6.2.2)

a multi-line text input area (inline)

<textarea rows="4" cols="20">
Type your comments here.

Checkboxes: <input> (6.2.3)

yes/no choices that can be checked and unchecked (inline)

<input type="checkbox" name="lettuce" /> Lettuce
<input type="checkbox" name="tomato" checked="checked" /> Tomato
<input type="checkbox" name="pickles" /> Pickles

Radio buttons: <input> (6.2.4)

sets of mutually exclusive choices (inline)

<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

Text labels: <label> (6.2.5)

<label><input type="radio" name="cc" value="visa" checked="checked" /> Visa</label>
<label><input type="radio" name="cc" value="mastercard" /> MasterCard</label>
<label><input type="radio" name="cc" value="amex" /> American Express</label>

Drop-down list: <select>, <option> (6.2.6)

menus of choices that collapse and expand (inline)

<select name="favoritecharacter">
	<option selected="selected">Kramer</option>

Using <select> for lists

<select name="favoritecharacter[]" size="3" multiple="multiple">
	<option selected="selected">Newman</option>

Option groups: <optgroup>

<select name="favoritecharacter">
	<optgroup label="Major Characters">
	<optgroup label="Minor Characters">

6.4: Processing Form Data in PHP

"Superglobal" arrays (6.4.1)

Example: Exponents

$base = $_REQUEST["base"];
$exp = $_REQUEST["exponent"];
$result = pow($base, $exp);

<?= $base ?> ^ <?= $exp ?> = <?= $result ?>
3 ^ 4 = 81

Example: Print all parameters

foreach ($_REQUEST as $param => $value) {

	<p>Parameter <?= $param ?> has value <?= $value ?></p>


Parameter name has value Marty Stepp

Parameter sid has value 1234567