Following is a form/processor which will do everything in one page thereby keeping things simple with a single copy. Attached code follows template approach which enables good amount of separation of php from html making things easy to follow. Feel free ask qns if you don't understand any aspects of this code. This is by no means a complete processor but just a sample foundation which can be easily built up.
PHP Code:
<?php
function insertError($name, $message)
{
$error = <<<ERROR
<tr>
<td class="error" colspan=2 >$message</td>
</tr>
ERROR;
return $error;
}
function initError($formFields)
{
foreach ($formFields as $key => $value)
{
$error[$value] = null;
}
return $error;
}
function initData($formFields)
{
foreach ($formFields as $key => $value)
{
$data[$value] = null;
}
return $data;
}
$formFields = array('media', 'name', 'address', 'city', 'state', 'postcode', 'country', 'email', 'phone');
$error = initError($formFields);
$data = initData($formFields);
$formError = false;
if (!empty($_POST))
{
foreach ($_POST as $key => $value)
{
$value = trim($value);
if (empty($value))
{
$formError = true;
$error[$key] = insertError($key, ucfirst($key) . " field was left empty");
}
# MORE VALIDATION TESTS CAN BE ADDED HERE
else
{
$data[$key] = $value;
}
}
if (isset($formError) and !$formError) # CHECK IF THERE WERE ERRORS IN FORM
{
# IF THERE ARE NO ERRORS START PROCESSING FORM DATA HERE
}
}
$form = <<<FORM
<form method="post" action="request.php">
<table class="table1" border="0">
<tr>
<td>Media type*:</td>
<td>
<input type="radio" id="booklet" name="media" value="booklet" /> Booklet
<input type="radio" id="CD" name="media" value="CD/DVD" /> CD/DVD
</td>
</tr>
${error['media']}
<tr>
<td class="c2"><label>Name*:</label></td>
<td><input name="name" id="name" type="text" value="${data['name']}" /></td>
</tr>
${error['name']}
<tr>
<td><label>Address*:</label></td>
<td><input name="address" id="address" type="text" value="${data['address']}" /></td>
</tr>
${error['address']}
<tr>
<td><label>Ciy/Town*:</label></td>
<td><input name="city" id="city" type="text" value="${data['city']}" /></td>
</tr>
${error['city']}
<tr>
<td><label>State*:</label></td>
<td><input name="state" id="state" type="text" value="${data['state']}" /></td>
</tr>
${error['state']}
<tr>
<td><label>Post Code*:</label></td>
<td><input name="postcode" id="postcode" type="text" value="${data['postcode']}" /></td>
</tr>
${error['postcode']}
<tr>
<td><label>Country*:</label></td>
<td>
<select name="country" id="country" size="1">
</select>
</td>
</tr>
${error['country']}
<tr>
<td><label>E-mail*:</label></td>
<td><input name="email" id="email" type="text" value="${data['email']}" /></td>
</tr>
${error['email']}
<tr>
<td><label>Telephone:</label></td>
<td><input name="phone" id="phone" type="text" value="${data['phone']}" /></td>
</tr>
${error['phone']}
</table>
<input name="catalogue" id="catalogue" type="submit" value="Request catalogue" />
</form>
FORM;
if (empty($_POST) or $formError)
{
# RETURNING BACK TO FORM IF THERE WERE ERRORS OR IF THE FORM IS BEING LOADED FOR THE FIRST TIME
?>
<!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=iso-8859-1" />
<meta name="author" content="Peter Selvaraj" />
<title>Untitled 1</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-size: 100%;
font-weight: normal;
}
body {
padding: 20px;
font: 65.2% Arial, Helvetica, sans-serif;
}
h2 {
font-size: 1.6em;
font-weight: bold;
margin-bottom: 1em;
}
form, p {
font-size: 1.2em;
margin-bottom: 1em;
}
form * {
font-size: 100%;
}
input {
padding: 3px;
}
table {
margin-bottom: 20px;
border-collapse: collapse;
}
td {
padding: 0.5em 0;
}
td.error {
color: #f00;
font-size: 0.9em;
padding: 0 0 10px 0;
}
</style>
</head>
<body>
<h2>Ordering a catalogue</h2>
<p>The items marked with a * are essential.</p>
<?php
echo $form; # PRINT FORM BLOCK
?>
</body>
</html>
<?php
}
?>
Bookmarks