This page contains an example of form in PHP
<html>
<head>
<style type="text/css">
.error {border: 1px solid #FF0000; background-color: #FFCC33;}
.blank {border: 1px solid #000000; background-color: #FFFFFF;}
.message {color: #FF0000;}
</style>
</head>
<body>
<?php
// CREATE STYLE VARIABLES
$forename_x = $surname_x = $age_x = $email_x = $tel_x = "blank" ;
// FORM SUBMITTED
if ($_POST) {
// SET FORM VARIABLES
foreach ($_POST as $fn => $v) {
$$fn = trim($v) ;
}
// CREATE EMPTY ERROR MESSAGE
$error = "" ;
// CHECK DATA ENTERED IN FIELDS
if ($forename == "") {
$error .="Please fill in forename. <br />" ;
$forename_x = "error" ;
}
if ($surname == "") {
$error .="Please fill in surname. <br />" ;
$surname_x = "error" ;
}
if ($email == "") {
$error .="Please fill in email. <br />" ;
$email_x = "error" ;
}
// VALIDATE AGE
if (is_numeric($age)== FALSE) {
$error .="Please enter a valid age (numbers only). <br />" ;
$age_x = "error" ;
}
// VALIDATE EMAIL
if ((strpos($email, "@") === FALSE) ||
(strpos($email, ".") === FALSE) ||
(strpos($email, " ") != FALSE)) {
$error .="Please enter a valid email address. <br />" ;
$email_x = "error" ;
}
// CLEAN AND VALIDATE TELEPHONE
$notel = array("+"," ", "(", ")","[", "]", "-", ",", "#") ;
$tel = str_replace($notel, array("00"), $tel) ;
if (is_numeric($tel) == FALSE) {
$error .="Please enter a valid telephone number. <br />" ;
$tel_x = "error" ;
}
// CLEAN BIOG
$biog = stripslashes($biog) ;
$biog = nl2br($biog) ;
}
// FORM NOT SUBMITTED OR INCORRECT
if ((!$_POST) || ($error != "")) {
?>
<div class="message"><?php echo $error ?></div>
<br />
<table border="0" cellpadding="3" cellspacing="0">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<tr>
<td>Forename:</td>
<td><input type="text" name="forename" size="45" maxlength="100" value="<?php echo $forename ?>" class="<?php echo $forename_x ?>" /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" name="surname" size="45" maxlength="100" value="<?php echo $surname ?>" class="<?php echo $surname_x ?>"/></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age" size="5" maxlength="5" value="<?php echo $age ?>" class="<?php echo $age_x ?>" /></td>
</tr>
<tr>
<?php
$sexf_sel = $sexm_sel = "";
if ($sex =="F") {
$sexf_sel = "CHECKED" ;
} elseif ($sex =="M") {
$sexm_sel = "CHECKED" ;
}
?>
<td>Sex:</td>
<td><input type="radio" name="sex" value="F" <?php echo $sexf_sel ?> />F   
<input type="radio" name="sex" value="M" <?php echo $sexm_sel ?> />M</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" size="45" maxlength="100" value="<?php echo $email ?>" class="<?php echo $email_x ?>"/></td>
</tr>
<tr>
<td>Telephone:</td>
<td><input type="text" name="tel" size="45" maxlength="20" value="<?php echo $tel ?>" class="<?php echo $tel_x ?>" /></td>
</tr>
<tr>
<td colspan="2">Describe yourself in a few words:</td>
</tr>
<tr>
<td colspan="2"><textarea name="biog" rows = "4" cols="47" value="<?php echo $biog ?>" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="send" /></td>
<td><input type="button" value="clear form" onclick="location.href='<?php echo $_SERVER['PHP_SELF'] ?>';" />
</tr>
</form>
</table>
<?php
// FORM SUBMITTED CORRECTLY
} else {
echo "$forename, thank you for submitting your form" ;
}
?>
</body>
</html>






