<!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">
<head>
<title>Guest Book Sign-in</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
h3
{
color: #C30;
}
input
{
color: #C30;
background-color: #FC9;
}
</style>
</head>
<body>
<?php
$FName = validateInput($_POST['FName'], "First name");
$LName = validateInput($_POST['LName'], "Last name");
$Email = validateInput($_POST['email'], "email");
if ($errorCount>0) {
echo "Please re-enter the information below.<br />\n";
redisplayForm($FName, $LName, $Email);
}
function displayRequired($fieldName) {
echo "The field \"$fieldName\" is required.<br />\n";
}
function validateInput($data, $fieldName) {
global $errorCount;
if (empty($data)) {
displayRequired($fieldName);
++$errorCount;
$retval = "";
}
else { //Only clean up the input if it isn't empty
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
$errorCount = 0;
if (isset($_POST['submit'])) {
$FName = stripslashes($_POST['FName']);
$LName = stripslashes($_POST['LName']);
$Email = stripslashes($_POST['email']);
// Replace any '~' characters
// with '-' characters
$FName = str_replace("~", "-", $FName);
$LName = str_replace("~", "-", $LName);
$Email = str_replace("~", "-", $Email);
$ExistingGuests = array();
if(file_exists("GuestBook/guests.txt") &&
filesize("GuestBook/guests.txt") > 0){
$GuestArray = file("GuestBook/guests.txt");
$count = count($GuestArray);
for ($i = 0; $i < $count; ++$i) {
$CurrGst = explode("~", $GuestArray[$i]);
$ExistingGuests[] = $CurrGst[0];
}
}
if (in_array($FName, $ExistingGuests)) {
echo "<p>The first name you entered already exixts!<br />\n";
echo "Your messge was not saved.</p>";
$FName = "";
}
else {
$GuestRecord = "$FName~$LName~$Email\n\r";
$GuestFile = fopen("GuestBook/guests.txt", "ab");
if ($GuestFile === FALSE)
echo "There was an error saving your information!\n\r";
else
{
fwrite($GuestFile, $GuestRecord);
fclose($GuestFile);
echo "Your information has been saved.\n\r";
$FName = "";
$LName = "";
}
}
}
else {
$FName = "";
$LName = "";
$Email = "";
}
?>
<h2 style="text-align:center">Guest Book Registry</h2>
<?php
function redisplayForm($firstName, $lastName) {
?>
<?php
}
?>
<h3>Post New Guest</h3>
<hr />
<form action="GuestBookSignIn.php" method="POST">
<span style="font-weight: bold">First Name:</span>
<input type="text" name="FName"
value="<?php echo $FName; ?>" /><br />
<span style="font-weight: bold">Last Name:</span>
<input type="text" name="LName"
value="<?php echo $LName; ?>" /><br />
<span style="font-weight: bold">Email:</span>
<input type="text" name="email"
value="<?php echo $Email; ?>" /><br />
<input type="submit" name="submit" value="Post Message" />
<input type="reset" name="reset" value="Reset Form" />
</form>
<hr />
<p>
<a href="GuestBookPostings.php">View Guests</a>
</p>
</body>
</html>
<?php // RAY_form_highlight_errors.php
error_reporting(E_ALL);
// DEMONSTRATE HOW TO HIGHLIGHT ERRORS IN FORM INPUT
// CLIENT IS ASKED TO PUT IN A VALUE
// IF THE VALUE FAILS OUR TEST WE SHOW AN ERROR MESSAGE
// WE PUT A MARKER NEXT TO THE INPUT CONTROL ON THE FORM
// WE TURN THE FORM BORDER RED
// SEE http://www.w3schools.com/CSS/pr_class_visibility.asp
// THESE CONDITIONS ARE SET FOR THE INITIAL ENTRY
$error_abc = 'hidden';
$boxer_abc = 'black';
$error_xyz = 'hidden';
$boxer_xyz = 'black';
$error_any = 'hidden';
// CAPTURE AND NORMALIZE THE POST VARIABLES - ADD YOUR OWN SANITY CHECKS HERE
$abc = (isset($_POST["abc"])) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = (isset($_POST["xyz"])) ? trim(strtoupper($_POST["xyz"])) : NULL;
// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
// VALIDATE THE 'abc' FIELD
if ($abc != 'ABC')
{
$error_any = 'visible';
$error_abc = 'visible';
$boxer_abc = 'red';
// $abc = NULL;
}
// VALIDATE THE 'xyz' FIELD
if ($xyz != 'XYZ')
{
$error_any = 'visible';
$error_xyz = 'visible';
$boxer_xyz = 'red';
// $xyz = NULL;
}
// DO WE HAVE INPUT FREE FROM ANY ERRORS?
if ($error_any != 'visible')
{
echo "CONGRATULATIONS";
die();
}
// OOPS - WE HAVE ERRORS
}
// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input style="border-color:$boxer_abc;" name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input style="border-color:$boxer_xyz;" name="xyz" value="$xyz" />
<input type="submit" />
</form>
ENDFORM;
// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
$FName = validateInput($_POST['FNam
$LName = validateInput($_POST['LNam
$Email = validateInput($_POST['emai
after the
if (isset($_POST['submit'])) {
$FName = stripslashes($_POST['FName
$LName = stripslashes($_POST['LName
$Email = stripslashes($_POST['email
// Replace any '~' characters
// with '-' characters
$FName = str_replace("~", "-", $FName);
$LName = str_replace("~", "-", $LName);
$Email = str_replace("~", "-", $Email);
For the second question I have no time to test to test it now, but I come back later :-)
Cheers