XHTML Forms

CSE 190 M (Web Programming), Spring 2008

University of Washington

References: JavascriptKit, w3schools

Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License.

Valid XHTML 1.0 Strict Valid CSS!

HTML forms

HTML form

HTML form: <form>

<form action="web service URL" method="get or post">
	<fieldset>
		form controls
	</fieldset>
</form>

Form example

<form action="http://www.foo.com/app.php" method="get">
	<fieldset>
		<label>Name: <input type="text" name="name" /></label>
		<label>Meal: <input type="text" name="meal" /></label>
		<label>Meat?
			<input type="checkbox" name="meat" />
		</label>
		<input type="submit" />
	<fieldset>
</form>




Recall: the HTML UI controls

<textarea name="message" rows="4" cols="20">
Type your comments here.
</textarea>
<select name="seinfeld">
	<optgroup label="Major Characters">
		<option value="Jerry">Jerry</option>
		<option value="George">George</option>
		<option value="Kramer">Kramer</option>
		<option value="Elaine">Elaine</option>
	</optgroup>
	<optgroup label="Minor Characters">
		<option value="Newman">Newman</option>
		<option value="Susan">Susan</option>
	</optgroup>
</select>
<input type="text" name="email" /><br />
<input type="password" name="pw" size="12" />

<label><input type="radio" name="cc" />Visa</label>
<label><input type="radio" name="cc" />MasterCard</label>
<label>
<input type="checkbox" name="lettuce" />
	Lettuce</label>
<label>
<input type="checkbox" name="tomato" />
	Tomato</label>

submit and reset buttons

<form action="http://www.foo.com/app.php" method="get">
	<fieldset>
		...
		<input type="submit" />
		<input type="reset" />
	</fieldset>
</form>

The name attribute

<form action="http://foo.com/app.php" method="get">
	<fieldset>
		Name: <input type="text" name="name" />
		Meal: <input type="text" name="meal" />
		<label>Meat? <input type="checkbox" name="meat" /></label>
		<input type="submit" />
	<fieldset>
</form>

submit, reset example





Problems with checkboxes, radio buttons

<form method="get"
 action="http://webster.cs.washington.edu/params.php" >
	<label><input type="checkbox" name="meat" /> Meat</label> <br />
	<label><input type="radio" name="creditcard" /> Visa</label>
	<label><input type="radio" name="creditcard" /> MasterCard</label> <br />

	Favorite Star Trek captain:
	<select name="startrek">
		<option>Kirk</option>
		<option>Picard</option>
	</select> <br />

	<input type="submit" />
</form>

The value attribute

<form method="get"
 action="http://webster.cs.washington.edu/params.php" >
	<label><input type="checkbox" name="meat" value="heckyeah"/> Meat</label> <br />
	<label><input type="radio" name="creditcard" value="visa" /> Visa</label>
	<label><input type="radio" name="creditcard" 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 />

	<input type="submit" />
</form>

Recall: get vs. post

<form action="http://www.foo.com/app.php" method="post">

Uploading files

Upload an image as your avatar:
<input type="file" name="avatar" />
<input type="submit" />
Upload an image as your avatar:

More about uploading files

<form action="http://foo.com/app.php"
 method="post" enctype="multipart/form-data">
	<fieldset>
		Upload an image as your avatar:
		<input type="file" name="avatar" />
		<input type="submit" />
	</fieldset>
</form>

Processing an uploaded file in PHP

Processing uploaded file, example

$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 {
	die "Error: required file not uploaded";
}

Hidden input parameters

Name: <input type="text" name="username" size="16" /> <br />
SID: <input type="text" name="sid" size="8" /> <br />

<input type="hidden" name="school" value="UW" />
<input type="hidden" name="quarter" value="08sp" />
<input type="submit" value="Retrieve student info" />
Name:
SID:

Recall: Styling form controls

element[attribute="value"] {
	...
}
input[type="text"] {
	color: blue;
	font-style: italic;
	border: 4px solid yellow;
}

Practice problem: 190M turnin form

Recreate the CSE 190 M Homework 4 (Fifteen Puzzle) turnin page. If you have time, write the PHP code to accept this form on the server. The server should save the student's files to a folder with the student's name and section. Submit the form to https://pascal.cs.washington.edu/cgi-bin/test_turnin.pl.

Required form parameter names: