Right now I am working on a registration and login form for a website. Right now I am trying to make sure I get the registration side working correctly before I start working on the login form.
The problem I am having is that I've received several complaints that this registration form doesn't display correctly, but I can't seem to figure it out why.
Below if the PHP code, the .inc code. And here is a link to the test registration form that this code displays.
Any help would be appreciated!
<?php
/* Program name: register_form.php
*/
ini_set("display_errors","on");
error_reporting(E_ALL | E_STRICT);
ini_set("include_path","./includes");
include("reginfo.inc"); /*This file contains the information for logging into the database*/
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
{
foreach($_POST as $field => $value)
{
if(empty($value))
{
if($field != "address2")
{
$blank_array[] = $field;
}
}
else
{
$good_data[$field] = strip_tags(trim($value));
}
}
if(@sizeof($blank_array) > 0)
{
/*Display error message if information is not entered*/
$message = "<p style='color: red; margin-bottom: 0;
font-weight: bold'>
You didn't fill in one or more required fields.
You must enter:
<ul style='color: red; margin-top: 0;
list-style: none' >";
foreach($blank_array as $value)
{
$message .= "<li>$value</li>";
}
$message .= "</ul>";
echo $message;
extract($good_data);
include("register_form.inc");
exit();
}
foreach($_POST as $field => $value)
{
if(!empty($value))
{
$user_patt = "/^[A-Za-z0-9_]{5,20}$/";
$pass_patt = "/(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{4,8})$/";
$name_patt = "/^[A-Za-z' -]{1,50}$/";
$address_patt = "/[0-9A-Za-z'. -]{5,50}$/";
$city_patt = "/^[A-Za-z' -]{1,50}$/";
$state_patt = "/(?i)^(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|GA||HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|TN|TX|
|UT|V[AIT]|W[AIVY])$/i";
$zip_patt = "/^([0-9]{5})(-[0-9]{4})?$/i";
$phone_patt = "/^[0-9)(xX -]{7,20}$/";
$email_patt = "/^[+_a-z0-9-]+(\.[+_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
$age_patt = "/^[0-9]{1}+[0-9]{1}$/";
if(preg_match("/user/i",$field))
{
if(!preg_match($user_patt,$value))
{
$error_array[] = "$value is not a valid name";
} //end of username check
}
if(!preg_match("/pass/i",$field))
{
if(preg_match($pass_patt,$value))
{
$error_array[] = "Please enter a password that is between 4 to 8 characters and contains at least an letter and number";
} //end of password check
}
if(preg_match("/name/i",$field))
{
if(!preg_match($name_patt,$value))
{
$error_array[] = "$value is not a valid name";
} //end of name check
}
if(preg_match("/address/i",$field))
{
if(!preg_match($address_patt,$value))
{
$error_array[] = "$value is not a valid address";
} //end of address check
}
if(preg_match("/city/i",$field))
{
if(!preg_match($city_patt,$value))
{
$error_array[] = "$value is not a valid City";
} //end of city check
}
if(preg_match("/state/i",$field))
{
if(!preg_match($state_patt,$value))
{
$error_array[] = "Please select a state from the drop down menu";
} //end of state check
}
if(preg_match("/zip/i",$field))
{
if(!preg_match($zip_patt,$value))
{
$error_array[] = "$value is not a valid zip code";
} //end of city check
}
if(preg_match("/phone/i",$field))
{
if(!preg_match($phone_patt,$value))
{
$error_array[] = "$value is not a valid phone number";
} //end of phone check
}
if(preg_match("/email/i",$field))
{
if(!preg_match($email_patt,$value))
{
$error_array[] = "$value is not a valid email address";
} //end of email check
}
}
$clean_data[$field] = strip_tags(trim($value));
}
if(@sizeof($error_array) > 0)
{
$message = "<ul style='color: red; list-style: none' >";
foreach($error_array as $value)
{
$message .= "<li>$value</li>";
}
$message .= "</ul>";
echo $message;
extract($clean_data);
include("register_form.inc");
exit();
}
else
{
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
or die("Couldn't connect to server");
foreach($clean_data as $field => $value)
{
$clean_data[$field] = mysqli_real_escape_string($cxn,$value);
}
$sql = "INSERT INTO Registration (user_name,password,email,first_name,last_name,phone,address,address2,city,zip,state)
VALUES ('$clean_data[user_name]','$clean_data[password]',
'$clean_data[email]','$clean_data[first_name]',
'$clean_data[last_name]','$clean_data[phone]',
'$clean_data[address]','$clean_data[address2]',
'$clean_data[city]','$clean_data[zip]',
'$clean_data[state]')";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute query");
include("regcomplete.inc");
}
}
else
{
include("register_form.inc");
}
?>
And the .inc file:
<?php
/* Program Name: register_form.inc
* Description: Registeration form for new users.
*/
$labels = array ("user_name" => "Desired User Name",
"password" => "Choose a Password",
"email" => "E-mail",
"first_name" => "First Name",
"last_name" => "Last Name",
"phone" => "Phone",
"address" => "Address Line 1",
"address2" => "Address Line 2",
"city" => "City",
"zip" => "Zip Code");
$states = array (" ","AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY");
$submit = "Register";
?>
<html>
<head>
<h1><img src="images/JBDaFsmall.png" alt="main_logo" width="293" height="187" align="middle" /></h1>
<style type="text/css">
<!--
.styleinfo {
color: #FFFFFF;
font-weight: bold;
background-color: #FF9900;
padding: 5px;}
#form {
margin-left: 40px;
margin-top: 40px;
padding-bottom: 20px;
}
-->
<!--
.style2info {
color: #FFFFFF;
font-weight: bold;
background-color: #00CCFF;
padding: 5px;}
#form {
margin-left: 40px;
margin-top: 40px;
padding-bottom: 20px;
}
-->
</style>
<p class="styleinfo">Please fill out the form below to register.</p>
<style type='text/css'>
<!--
form {
margin: 1.5em 0 0 0;
padding: 0;
}
#field {padding-bottom: .2em;}
label {
font-weight: bold;
float: left;
width: 15%;
margin-right: .5em;
text-align: right;
}
#submit {
margin-left: 30%;
}
-->
</style>
</head>
<?php
/* Nothing more then a display form loop */
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
foreach($labels as $field => $label)
{
if($field == "password")
{
echo "<div class='field'><label for='$field'>$label</label>
<input id='$field' name='$field' type='password' value='".@$$field."'
size='50%' maxlength='65' /></div>\n";
}
else
{
echo "<div class='field'><label for='$field'>$label</label>
<input id='$field' name='$field' type='text' value='".@$$field."'
size='50%' maxlength='65' /></div>\n";
}
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
}
?>
<div class="field"><label for='$field'>State</label>
<select name="state">
<?php
foreach ($states as $state)
{
$sel = '';
if (isset($_POST['state']) AND $_POST['state'] == $state)
{
$sel = 'selected="selected"';
}
echo '<option value="'.$state.'" '.$sel.'>'.$state.'</option>';
}
?>
</select>
</div>
<?php
echo "<div id='submit'>
<input type='hidden' name= 'submitted' value='yes'>
<input type='submit' name='contactButton' value='$submit'></div>"
;
?>
</form>
<p class="style2info">JB Design & Fabrication Registration Form</p>
</body>
</html>