Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
A form gathers the information from your visitor, and the script processes that information. The script can log the information to a database on the server, send the information via email, or perform any number of other functions.
In this chapter, since the focus is on creating Web forms, we’ll use a very simple PHP script to echo the data back to the visitor when they fill out and submit a form . I’ll also give you a script that you can use to submit a form’s contents to your email address
.
Here is the script used to process the forms in this chapter. Notice that the PHP script lives right in an HTML page. (You can find a commented version of this script at www.bruceontheloose.com/htmlcss/examples/.)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Processing Form Data</title>
<style type="text/css">
body {
font-size: 100%;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<p>This is a very simple PHP script that outputs the name of each bit of information (that corresponds to the <code>name</code> attribute for that field) along with the value that was sent with it right in the browser window.</p>
<p>In a more useful script, you might store this information in a MySQL database, or send it to your email address.</p>
<table>
<tr><th>Field Name</th><th>Value(s)</th></tr>
<?php
if (empty($_POST)) {
print "<p>No data was submitted.</p>";
} else {
foreach ($_POST as $key => $value) {
if (get_magic_quotes_gpc()) $value=stripslashes($value);
if ($key=='extras') {
if (is_array($_POST['extras']) ){
print "<tr><td><code>$key</code></td><td>";
foreach ($_POST['extras'] as $value) {
print "<i>$value</i><br />";
}
print "</td></tr>";
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
} else {
print "<tr><td><code>$key</code></td><td><i>$value</i></td></tr>\n";
}
}
}
?>
</table>
</body>
</html>