Anytime you create a registration form, you should validate the form elements using, HTML attributes (e.g. required = "required"), client-side JavaScript, server-side script or a combination of one of more of these. You want to ensure that all critical form elements get a value or that they are the right data type. This is especially true of text input fields.
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2> <?php
print "<p>Register for benefits</p>";
// Check to see if the form has been submitted:
if (isset($_POST["Submit"]) )
{
$missingField = FALSE; // Set $missingField FLAG to TRUE if "if" statement is TRUE.
// Check each form variable to see if any one is empty
if (empty($_POST["FirstName"]) && isset($_POST["FirstName"]))
{
$missingField = TRUE;
print '<p class="error">Please enter your first name!</p>';
}
} ?>
<?php
print "<p>Register for benefits</p>";
// Check to see if the form has been submitted:
if (isset($_POST["Submit"]) )
{
$missingField = FALSE; // Set $missingField FLAG to TRUE if "if" statement is TRUE.
// Check each form variable to see if any one is empty
if (empty($_POST["FirstName"]) && isset($_POST["FirstName"]))
{
$missingField = TRUE;
print '<p class="error">Please enter your first name!</p>';
}
if (empty($_POST["LastName"])) { $missingField = TRUE; print '<p class="error">Please enter your last name!</p>'; } if (empty($_POST["Email"])) { $missingField = TRUE; print '<p class="error">Please enter your email!</p>'; } } ?>
<?php
print "<p>Register for benefits</p>";
// Check to see if the form has been submitted:
if (isset($_POST["Submit"]) )
{
$missingField = FALSE; // Set $missingField FLAG to TRUE if "if" statement is TRUE.
// Check each form variable to see if any one is empty
if (empty($_POST["FirstName"]) && isset($_POST["FirstName"]))
{
$missingField = TRUE;
print '<p class="error">Please enter your first name!</p>';
}
if (empty($_POST["LastName"])) { $missingField = TRUE; print '<p class="error">Please enter your last name!</p>'; } if (empty($_POST["Email"])) { $missingField = TRUE; print '<p class="error">Please enter your email!</p>'; } if (empty($_POST["Password1"]))
{ $missingField = TRUE; print '<p class="error">Please enter a password!</p>'; }
if ($_POST["Password1"] != $_POST["Password2"]) { $missingField = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; }
if (!$missingField) { print "<p>You are now registered!<br />Okay, you are not really registered YET...</p>"; // Clear posted values by emptying the $_POST array $_POST = array(); } } ?
<?php /*?><?php
if ( empty( $_POST ) )
{ ?> <?php */?>
<form action="registration_page.php" method="post"> ... ... ... ... </form>
<?php /*?><?php
}
else
{
$FirstName = $_POST[ 'FirstName' ];
$LastName = $_POST[ 'LastName' ];
$Email = $_POST[ 'Email' ];
$Password = $_POST[ 'Password1' ];
$sql = "INSERT INTO passwords ( FirstName, LastName, Email, Password )
VALUES ( :FirstName, :LastName, :Email, :Password )";
$query = $pdo_conn->prepare( $sql );
$result = $query->execute( array( ':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Email'=>$Email, ':Password'=>$Password ) );
if($result)
{
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
else
{
echo "<p>Sorry, there has been a problem registering. Please contact admin.</p>";
}
}
?><?php */?>
<?php /*?><?php
}
else
{
$FirstName = $_POST[ 'FirstName' ];
$LastName = $_POST[ 'LastName' ];
$Email = $_POST[ 'Email' ];
$Password = $_POST[ 'Password1' ];
$sql = "INSERT INTO passwords ( FirstName, LastName, Email, Password )
VALUES ( :FirstName, :LastName, :Email, :Password )";
$query = $pdo_conn->prepare( $sql );
$result = $query->execute( array( ':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Email'=>$Email, ':Password'=>$Password ) );
if($result)
{
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
else
{
echo "<p>Sorry, there has been a problem registering. Please contact admin.</p>";
}
}
?><?php */?>
if (!$missingField)
{
// print "<p>You are now registered!<br />Okay, you are not really registered YET...</p>";
// Clear posted values by emptying the $_POST array
// $_POST = array();
$FirstName = $_POST[ 'FirstName' ];
$LastName = $_POST[ 'LastName' ];
$Email = $_POST[ 'Email' ];
$Password = $_POST[ 'password1' ];
$sql = "INSERT INTO passwords ( FirstName, LastName, Email, Password )
VALUES ( :FirstName, :LastName, :Email, :Password )";
$query = $pdo_conn->prepare( $sql );
$result = $query->execute( array( ':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Email'=>$Email, ':Password'=>$Password ) );
if($result)
{
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a><br/><br/>");
}
else
{
echo "<p>Sorry, there has been a problem registering. Please contact admin.</p>";
}
}
}
?>
<?php /*?><?php... ...
}
else
{}
?><?php */?>
<?php /*?><?php
if ( empty( $_POST ) )
{ ?> <?php */?>
<form action="registration_page.php" method="post">
A Sticky Form is a form that remembers the values that were entered into it if the form is not submitted correctly so that a user does not have to re-enter those values. The syntax for a form variable to be sticky is:
<input type="text" name="FirstName" size="20" value="<?php if (isset($_POST["FirstName"])) {print htmlspecialchars($_POST["FirstName"]); } ?>"
The value attribute of a form variable is set to a PHP script that check to see if the variable is set and if so it will display the result in the form element after parsing it with the htmlspecialchars() method. The htmlspecialchars method converts certain HTML tags into entities (e.g., <h1> entity is <h1>) to avoid problems if a user enters HTML tags into a form field.
<form action="registration_page.php" method="post">
<label for="FirstName">First Name:</label>
<input name="FirstName" type="text" value =
"<?php if (isset($_POST["FirstName"])) {print htmlspecialchars($_POST["FirstName"]);} ?>" size="20" data-mini="true"/>
<label for="LastName">Last Name:</label>
<input name="LastName" type="text" value = "<?php if (isset($_POST["LastName"])) {print htmlspecialchars($_POST["LastName"]);} ?>" size="20" data-mini="true"/>
<label for="Email">Email Address:</label>
<input name="Email" type="text" value = "<?php if (isset($_POST["Email"])) {print htmlspecialchars($_POST["Email"]);} ?>" size="20" data-mini="true" />
<label for="Password1">Password:</label>
<input type="password" required="required" name="Password1" size="20"data-mini="true"/>
<label for="Password2">Re-enter Password:</label>
<input type="password" required="required" name="Password2" size="20"data-mini="true"/>
<p><input type="submit" name="Submit" value="REGISTER" data-inline="true" data-mini="true"/></p>
</form>