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>
A sample form
</title>
</head>
<body>

<form id="theform" method="post" 
action="form_handle.php">

<table style="border: none">
<tr>
<td>
Your name goes here:
</td>
<td>
<input type="text" name="name" value="Joe" style="width: 30" />
</td>
</tr>

<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" style="width: 30" />
</td>
</tr>
<tr>

<td>
<input type="submit" value="submit" />
</td>
</tr>
</table>
</form>

</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/forms/form.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">
<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" />
<!-- 
Processing form data in PHP
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/3/08
--> 

<?php
$name = $_POST["name"];
$food = $_POST["food"];
?>
<title>
Form data processing	
</title>
</head>
<body>
<p>
<?php
print "$name likes $food!!!<br/>\n";
?>

</p>
</body>
</html>

An example with more form elements

The form:


<!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>More form elements</title>
</head>
<body>
<h1>A form with more elements</h1>
<form id="theform" method="post"
action="more_form_elements.php">
<table style="border: none">
<tr>
<td>
What is your name?
</td>
<td>
<input type="text" name="name" value="Joe" style="width: 30" />
</td>
</tr>
<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" style="width: 30" />
</td>
</tr>
<tr>
<td>
What is your favorite season? Check all that applies.
</td>
<td>
<input type="checkbox" name="winter" value="winter" /><br />
<input type="checkbox" name="spring" value="spring" /><br />
<input type="checkbox" name="summer" value="summer" /><br />
<input type="checkbox" name="fall" value="fall" />
</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" value="submit" />
</td>
</tr>
</table>
</form>

</body>
</html>

The php handler:


<!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"];
?>
<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>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/forms/more_form_elements.html


UMM CSci 1101

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.