I know why this is happening and even on what line of code it is, but I can't come up with a way around it.
Here's my whole code:
<?php // Recive the posted form and present the form again to visitor if validation fails
// Start with the PHP code
$forename = $surname = $username = $password = $age = $email = "";
if(isset($_POST['forename']))// check if $_POST['forname'] has been set from a form yet, if so fix it and set it equal to $forename
$forename = fix_string($_POST['forename']);
if(isset($_POST['surname']))
$surname = fix_string($_POST['surname']);
if(isset($_POST['username']))
$username = fix_string($_POST['username']);
if(isset($_POST['password']))
$password = fix_string($_POST['password']);
if(isset($_POST['age']))
$age = fix_string($_POST['age']);
if(isset($_POST['email']))
$email = fix_string($_POST['email']);
$fail = validate_forename($forename);// call function to validate the forename from $_POST. If it's validated return empty string, if not return error message
$fail .= validate_surname($surname);// add on to the error message if one exists...
$fail .= validate_username($username);
$fail .= validate_password($password);
$fail .= validate_age($age);
$fail .= validate_email($email);
echo "<html><head><title>An Example Form</title>";
if($fail == "")// if there were no errors
{
// print out what was entered into all the fields
echo "<body>Form data successfully validated: $forename, $surname, $username, $password, $age, $email.</body>";//------!took out </head in front and </html>
// This is where you would enter posted fields into a database
exit;
}
// Now output the HTML and Javascript code
echo <<<_END
<!-- The HTML Section -->
<style>.signup {border: 1px solid #999999;
font: normal 14px helvetica; color #444444; } </style>
<script>//Javascript validation methods called here
/*
Each call to a function in the validate function will return an empty string
if the field validates, or an error message if it fails. If there are any errors it
will not return true and an alert will pop up displaying the errors
*/
function validate(form)
{
fail = validateForename(form.forename.value)
fail += validateSurname(form.surname.value)
fail += validateUsername(form.username.value)
fail += validatePassword(form.password.value)
fail += validateAge(form.age.value)
fail += validateEmail(form.email.value)
if(fail == "") return true// if it returns true the form will be allowed to be submitted
else
{
alert(fail)
return false// if it returns false an alert pops up and the user can make changes after closing out
}
}
</script></head><body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="eeeeee">
<th colspan="2" align="center">Signup Form</th>
<tr><td colspan="2">Sorry the following errors were found<br />
in your form: <p><font color=red size=1><i>$fail</i></font></p></td></tr>
<form method="post" action="adduser.php"
onsubmit="return validate(this)"><!-- call this function when the form is submitted if true, the form can be submitted-->
<tr><td>Forename</td><td><input type="text" maxlength="32"
name="forename" value="$forename" /></td></tr>
<tr><td>Surname</td><td><input type="text" maxlength="32"
name="surname" value="$surname" /></td></tr>
<tr><td>Username</td><td><input type="text" maxlength="32"
name="username" value="$username" /></td></tr>
<tr><td>Password</td><td><input type="text" maxlength="32"
name="password" value="$password" /></td></tr>
<tr><td>Age</td><td><input type="text" maxlength="32"
name="age" value="$age" /></td></tr>
<tr><td>Email</td><td><input type="text" maxlength="32"
name="email" value="$email" /></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Signup" /></td></tr></form></table>
<!-- The Javascript section -->
<script type="text/javascript">
function validateForename(field)
{
if(field == "") return "No Forename was entered.\n"// not entering anything is an error, return an error message
return ""// else return empty string
}
function validateSurname(field)
{
if(field == "") return "No Surname was entered.\n"// not entering anything is an error, return an error message
return ""// else return empty string
}
function validateUsername(field)
{
if(field == "") return "No Username was entered.\n"// not entering anything is an error, return an error message
else if(field.length < 5)// if the length of the entered username is less than 5 characters it's an error, return error message
{
return "Usernames must be at least 5 characters.\n"
}
else if(/[^a-zA-Z0-9_-]/.test(field))// regular expression to check if there is anything else besides a-Z,A-Z,0-9,_ and -. If so, error
{
return "Only a-z, A-Z, 0-9 - and _ allowed in Usernames.\n"
}
return ""// if nothing wrong with entered username, return empty string
}
function validatePassword(field)
{
if(field == "") return "No Password was entered.\n"// not entering anything is an error, return an error message
else if(field.length < 6)// if the length of the entered password is less than 6 characters it's an error, return error message
{
return "Passwords must be at least 6 characters.\n"
}
else if(!(/[a-z]/.test(field) && /[A-Z]/.test(field) && /[0-9]/.test(field)))//regular expression to check if password contains at least one of each, if not it's an error
{
return "Passwords require one of each of a-z, A-Z, and 0-9.\n"
}
return ""// if nothing wrong with entered password, return empty string
}
function validateAge(field)
{
if(isNaN(field)) return "No Age was entered.\n"// not entering a number is an error, return an error message
else if(field < 18 || field > 110)// entering an age under 18 or greater than 110 is an error, return error message
{
return "Age must be between 18 and 110.\n"
}
return ""// if nothing wrong with entered age, return empty string
}
function validateEmail(field)
{
if(field == "") return "No Email was entered.\n"// not entering anything is an error, return an error message
else if(!((field.indexOf(".") > 0) && (field.indexOf("@") > 0)) && /[\W]/.test(field))// if . and @ are first letter or not there at all or if doesn't contain the stuff, it's an error
{
return "The email address is invalid.\n"
}
return ""// if nothing wrong with entered password, return empty string
}
</script></body></html>
_END;
// Now implement the PHP functions
function validate_forename($field)
{
if($field == "") return "No Forename was enetered<br />";
return "";
}
function validate_surname($field)
{
if($field == "") return "No Surname was entered<br />";
return "";
}
function validate_username($field)
{
if($field == "") return "No Username was entered<br />";
else if(strlen($field) < 5)
{
return "Usernames must be at least 5 characters<br />";
}
else if(preg_match("/[^a-zA-Z0-9_-]/", $field))
{
return "Only letters, numbers, - and _ in usernames<br />";
}
return "";
}
function validate_password($field)
{
if($field == "") return "No Password was entered<br />";
else if(strlen($field) < 6)
{
return "Passwords must be at atleast 6 characters<br />";
}
else if(!preg_match("/[a-z]/", $field) ||
!preg_match("/[A-Z]/", $field) ||
!preg_match("/[0-9]/", $field))
{
return "Passwords require 1 of each of a-z, A-Z, and 0-9<br /";
}
return "";
}
function validate_age($field)
{
if($field == "") return "No Age was entered<br />";
else if($field < 18 || $field > 110)
{
return "Age must be between 18 and 110<br />";
}
return "";
}
function validate_email($field)
{
if($field == "") return "No Email was entered<br />";
else if(!((strpos($field, ".") > 0) &&
(strpos($field, "@") > 0)) ||
preg_match("/[^a-zA-Z0-9.@_-]/", $field))
{
return "The Email address is invalid<br />";
}
return "";
}
// Function to sanitize each field and prevent code injection
function fix_string($string)
{
if(get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities($string);
}
?>
This is where the problem is occurring specifically:
First time through these will all come out with error messages because the variables have not been set with the $_POST array yet:
$fail = validate_forename($forename);// call function to validate the forename from $_POST. If it's validated return empty string, if not return error message $fail .= validate_surname($surname);// add on to the error message if one exists... $fail .= validate_username($username); $fail .= validate_password($password); $fail .= validate_age($age); $fail .= validate_email($email);
And then since those have the error messages, this is called in the HTML section:
<tr><td colspan="2">Sorry the following errors were found<br /> in your form: <p><font color=red size=1><i>$fail</i></font></p></td></tr>
I'm sure this is an easy fix, but I've been thinking about it for a little while now and can't figure out a way around it.
Thanks for the help!

New Topic/Question
Reply




MultiQuote









|