Examples of php form handling

A form file (html, could be php as well)


<html>
<body>

<form name="theform" method="POST" 
action="form.php">
<table style="border: none">
<tr>
<td>
Your name goes here:
</td>
<td>
<input type="text" name="name" value="Joe" width="30">
</td>
</tr>
<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" width="30">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>

</body>
</html>

A php file for handling this 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: 3/28/06 
--> 
<?php
$name = $_POST["name"];
$food = $_POST["food"];
?>
<html>
<head>
<title>
Form data processing	
</title>
</head>
<body>
<?php
print "$name likes $food!!!<br/>\n";
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring07/forms/form.html

More form elements


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>More form elements</title>
</head>
<body>
<h1>A form with more elements</h1>
<form name="theform" method="POST" 
action="more_form_elements.php">
<table style="border: none">
<tr>
<td>
Your name goes here:
</td>
<td>
<input type="text" name="name" value="Joe" width="30">
</td>
</tr>
<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" width="30">
</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>

Handling the form elements


<!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"];
$about = $_POST["about"];
$survey = $_POST["survey"];
?>
<html>
<head>
<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>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://rynite.morris.umn.edu/~elenam/1101_spring07/forms/more_form_elements.html