Is there a simple way to repopulate a form when a user puts in unacceptable data (say an email address without the the @) and submits the form.) In the attached code, the form displays an error message, and re presents the form but all the fields are blank. The user has to rekey all the information back in. What a pain.
Any help would be greatly appreciated.
<?php
session_start();
require("../functions/checkEntries.php");
include_once "../DB.php";
include_once "../style_sheet.php";
/*This script allows the user to set up a administration account to log in. */
if(empty($_SESSION['okay2process'])) {
header("Location: coach_validation.php");
exit();
}
//---| New User constants
$accessLvL = 1;
$status = 1;
$dateEntered = date("Ymd");
// $ip=@$REMOTE_ADDR;
//---CHECK DATA to be sure it is clean
if(isset($_POST['submit'])) {
$nameF = protect($_POST['nameF']);
$nameL = protect($_POST['nameL']);
$userName = protect($_POST['userName']);
$userPass = protect($_POST['userPass']);
$passconf = protect($_POST['passconf']);
$confirm = protect($_POST['passconf']);
$email = protect($_POST['email']);
$whatDiv = $_POST['whatDiv'];
$userPass = sha1($userPass);
$confirm = sha1($confirm);
//---| ERROR CHECKING
$errors = array();
if(!$userName)
{
$errors[] = "Please enter a user name!";
}
if(!$userPass)
{
$errors[] = "Password is not defined!";
}
if($userPass)
{
if(!$confirm)
{
$errors[] = "Confirmation password is not defined!";
}
}
if(!$email)
{
$errors[] = "E-mail is not defined!";
}
if(!$nameF)
{
$errors[] = "First Name is not defined!";
}
if(!$nameL)
{
$errors[] = "Last Name is not defined!";
}
if(strlen($whatDiv) > 7)
{
$errors[] = "You must indicate division!";
}
if($userName)
{
if(!ctype_alnum($userName))
{
$errors[] = "Username can only contain numbers and letters!";
}
$range = range(6,30);
if(!in_array(strlen($userName),$range))
{
$errors[] = "Username must be between 6 and 30 characters!";
}
}
if($userPass && $passconf)
{
if($userPass != $confirm)
{
$errors[] = "Passwords do not match!";
}
}
if($email)
{
$checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
if(!preg_match($checkemail, $email))
{
$errors[] = "E-mail is not valid, must be name@server.tld!";
}
}
if($nameF)
{
$range2 = range(1,35);
if(!in_array(strlen($nameF),$range2))
{
$errors[] = "Your First name must be between 1 and 35 characters!";
}
}
if($nameL)
{
$range5 = range(1,35);
if(!in_array(strlen($nameL),$range5))
{
$errors[] = "Your Last name must be between 5 and 35 characters!";
}
}
if($userName)
{
$sql = "SELECT * FROM `admin_coach` WHERE `userName`='".$userName."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0)
{
$errors[] = "Either the <font color =\"black\">user name </font>you have chosen is taken by another coach, or you are
already registered and cannot set up another account. Log in through thge HOME page!";
}
}
if($email)
{
$sql2 = "SELECT * FROM `admin_coach` WHERE `email`='".$email."'";
$res2 = mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res2) > 0)
{
$errors[] = "The e-mail address you supplied has been taken by another user!";
}
}
//---| Data is acceptable INSERT into database
if(count($errors) == 0)
{
$sql4 = "INSERT INTO admin_coach
(division, userName, userPass, nameF, nameL, email, accessLvL, status, dateEntered)
VALUES
(
'$whatDiv',
'$userName',
'$userPass',
'$nameF',
'$nameL',
'$email',
'$accessLvL',
'$status',
'$dateEntered '
)";
$res4 = mysql_query($sql4) or die(mysql_error());
$closingMessage = 1;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="../../templates/master_style_sheet.css" />
</head>
<body>
<img src="../artwork/CupLogo.png" width="265" height="94" style="float: left" /><p class="red_italic_text_large">
</p>
<p class="red_italic_text_large">New Application. Set up account </p>
<hr>
<p class="bullet_sentence">This page is for first time visitors who want to
submit an application. If you already have a user name and a password and you
are trying to access your personal or team information, please exit and go to
the HOME page. </p>
<p class="bullet_sentence">Be sure to make a note of your user name and
password. If you have lost your user name or password, <br>
go to the Home page and click on Can't Access Account [doesn't work yet]</p>
<p class="bullet_sentence">Use your TAB KEY to enter data. </p>
<?php
if(count($errors) > 0){
echo "ERROR MESSAGES<br/>" ;
//echo "<a href='javascript:history.back()'>back to previous page</a>";
//echo "<a href='$HTTP_REFERER'>return to previous page</a>";
echo "<br/>";
foreach($errors AS $error)
{
echo "<font color =\"red\">" . $error . "</font><br>\n";
}
}
?>
<table class ="bullet_sentence" border="0" cellspacing="3" cellpadding="3">
<form method="post" action="coach_sign_up.php">
<tr><td>
First Name</td><td><input type="text" size = "25" maxlength="35" name="nameF"></td></tr>
<tr><td>
Last Name</td><td><input type="text" size = "25" maxlength="35" name="nameL"></td></tr>
<tr><td>
User Name</td><td><input type="text" size = "25" maxlength="30" name="userName"></td></tr>
<tr><td>
Password</td><td><input type="password" size = "25" maxlength="30"name="userPass"></td></tr>
<tr><td>
Confirm</td><td><input type="password" name="passconf"></td></tr>
<tr><td>
E-Mail</td><td><input type="text" name="email"></td></tr>
<tr><td>
Enter your division </td><td><input type ="hidden" value="whatDiv">
<select name="whatDiv">
<option>Please Choose Your Division </option>
<option value ="U14G"> U14 Girls </option>
<option value ="U12G"> U12 Girls </option>
<option value ="U10G"> U10 Girls </option>
<option value ="U14B"> U14 Boys </option>
<option value ="U12B"> U12 Boys</option>
<option value ="U10B"> U10 Boys </option>
</select></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Register"></td></tr>
</form></table>
<?php if( $closingMessage == 1) { ?>
<p class="bullet_sentence">You have successfully registered.
The next step is to fill in the application:</p>
<span class="bullet_sentence">
<a href=\"app_intro.php\">fill in the Tournament Application</a></span><p class="bullet_sentence"><a href=\"www.web.org\">Exit. You can return later to fill in the application</a></p>
<?php }?>
</body>
</html>