Working with forms

The form (here is .html, can be .php)


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>XHTML form</title>
</head>
<body>
<h1>An XHTML form </h1>
<form id="theform" method="post" action="form_handling.php">
<table style="border: none">
<tr>

<td>
What is your name?
</td>
<td>
<input type="text" name="name" value="Joe" style="width: 160px" 
/>
</td>
</tr>
<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" style="width: 160px" />
</td>
</tr>
<tr>
<td>
What is your favorite season? Check all that applies.
</td>
<td>
<input type="checkbox" name="winter" value="winter" /> Winter<br />
<input type="checkbox" name="spring" value="spring" /> Spring<br />
<input type="checkbox" name="summer" value="summer" /> Summer<br />
<input type="checkbox" name="fall" value="fall" /> Fall <br />
</td>
</tr>
<tr>
<td>Tell us about yourself</td>
<td><textarea rows="2" cols="20" name="about">
</textarea>
</td>
</tr>
<tr>
<td>This survey was:</td> 
<td>
Interesting

<input type="radio" checked="checked"
name="survey" value="interesting" />
<br />
Boring <input type="radio" 
name="survey" value="boring" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
http://csci4657spr11.morris.umn.edu/~elenam/4657sp11/form_handling.html

The php script that handles the form:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Processing form data in PHP
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 4/9/07
-->
<?php
$name = $_POST["name"];
$food = $_POST["food"];
$winter = $_POST["winter"];
$spring = $_POST["spring"];
$summer = $_POST["summer"];
$fall = $_POST["fall"];
$about = $_POST["about"];
$survey = $_POST["survey"];
$submit = $POST["submit"];
?>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Form data processing
</title>
</head>
<body>
<?php
print "<h3>Test printing</h3>\n";
print "<p>";
print_r($_POST);
print "</p>\n";

print "<h3>Pretty printing</h3>\n";
print "<p>$name likes $food!</p>\n";
print "<p>";
if (isset($winter))
  $season = $winter;
if (isset($spring))
  if (isset($season)) $season = $season.", ".$spring;
  else $season = $spring;
if (isset ($summer))
  if (isset($season)) $season = $season.", ".$summer;
  else $season = $summer;
if (isset ($fall))
  if (isset($season)) $season = $season.", ".$fall;
  else $season = $fall;
print "$name likes these seasons: $season<br />\n";
print "</p>";
print "<p>Here is what $name tells us about him/herself:<br />";
print "$about</p>\n";

if ($survey == "interesting") {
  print "<p>We are glad that $name found the survey intersting</p>\n";
 } else {
  print "<p>We are sorry that $name found the survey boring</p>\n";
 }
?>
</body>
</html>

UMM CSci 4657

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.