Dear Professional PHP Coders,
I really need your help here.
I am designing a form for registration (Please find the codes below).
There are two errors I want corrected on this form.
1. PostBack data on controls.
If a user submits a form, and there are errors on the submitted data, php should postback the user's submitted data on the controls. Instead of clearing them (i.e., setting the value to empty).
2. Display an array of all errors during form submission.
Here, if a user has failed to provide necessary data on a selected controls, php should create an array of all controls wherein there are incorrect data, and php should echo the array of such incorrect errors below the form.
(pls find the code below).
I have tried something, but php is not echoing the assumed errors. No reporting at all.
Please kindly help me.
<?php
//Connect to the database through our include
include_once "/dat/connString_local.php";
//include the php file that checks whether a user is logged in or not
include_once("toplinks_for_join_form.php");
//load the captcha code from the database
$sql = mysql_query("SELECT id, captchacode, status FROM captcha");
while ($result = mysql_fetch_array($sql))
{
$id = $result['id'];
$captchacode_from_dbase = $result['captchacode'];
}
// echo $captchacode_from_dbase;
// Set error message as blank upon arrival to page
$errorMsg = array();
// First we check to see if the form has been submitted
if (isset($_POST['username'])){
// Filter the posted variables
// GET USER IP ADDRESS AND LOCATION DATA
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$address1 = str_replace("[^A-Z a-z0-9]", "", $_POST['address1']);
$address2 = str_replace("[^A-Z a-z0-9]", "", $_POST['address2']);
$address = $address1 .", ". $address2 ." "; //CONCATENATE ADDRESS FIELDS INTO ONE VARIABLE
$country = str_replace("[^A-Z a-z0-9]", "", $_POST['country']); // filter everything but spaces, numbers, and letters
$state = str_replace("[^A-Z a-z0-9]", "", $_POST['state']); // filter everything but spaces, numbers, and letters
$city = str_replace("[^A-Z a-z0-9]", "", $_POST['city']); // filter everything but spaces, numbers, and letters
$address .=$city. ", " .$state. ", " .$country;
//account info
if($_POST['accounttype']=="atm"){
$accounttype = "ATM";}else {$accounttype = "eCurrency";}
//$accounttype = str_replace("[^a-z]", "", $_POST['accounttype']); // filter everything but lowercase letters
$user_captcha = str_replace("[^0-9]", "", $_POST['captchacode']);//filter everything except numbers only
//login data
$username = str_replace("[^A-Za-z0-9]", "", $_POST['username']); // filter everything but numbers and letters
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = str_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
$password2 = str_replace("[^A-Za-z0-9]", "", $_POST['password2']); // filter everything but numbers and letters
//bio data
$fullname = str_replace("[^A-Za-z]", "", $_POST['fullname']); // filter everything but upper and lowercase letters
//find the gender
if(($_POST['gender'])=="male"){
$gender = "m";} else {$gender = "f"; }
//concatenate the date of birth fields
$day = str_replace("[^0-9]", "", $_POST['day']);//filter everything except numbers only
$month = str_replace("[^0-9]", "", $_POST['month']);//filter everything except numbers only
$year = str_replace("[^0-9]", "", $_POST['year']);//filter everything except numbers only
$dob = $year."/".$month."/".$day; //mysql accepts date string starting from year, month then day.
$bio = str_replace("[^A-Z a-z0-9]", "", $_POST['bio']);
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if(isset($_POST['btn_register'])){
if(!$fullname){$errorMsg[] .= "---Full Name must be characters only and not spaces.<br />";}//die($errorMsg.);
else if(!isset($_POST['gender'])){$errorMsg = "---Please select gender.<br />";}//die($errorMsg.=
else if(!$day){$errorMsg[] .= "---You must enter your valid date of birth.<br />";}//die($errorMsg.
else if(!$month){$errorMsg[] .= "---You must enter your valid date of birth.<br />";}//die($errorMsg.=
else if(!$year){$errorMsg[] .= "---You must enter your valid date of birth.<br />";}//die($errorMsg.=
else if(!$bio){$errorMsg[] .= "---Please tell us about yourself.";}//die($errorMsg.=
else if(strlen($bio) < 100){$errorMsg[] = "---Please tell us about yourself. Minimum of 100 xters.<br />";}//die(
else if(!$address1){$errorMsg[] .= "---Address is compulsory please.<br />";}//die($errorMsg.=
else if(!$city){$errorMsg[] .= "---Please enter your city location.<br />";}//die($errorMsg.=
else if(!$state){$errorMsg[] .= "---Please enter or select a state where you come from.<br />";}//die($errorMsg.=
else if(!$country){$errorMsg[] .= "---Please select your country from the list.<br />";}//die($errorMsg.=
else if(!$email){$errorMsg[] .= "---You must enter a valid email address.<br />";}//die($errorMsg.=
else if(!$username){$errorMsg[] .= "---Your username is required please.<br />";}//die($errorMsg.=
else if(!$password){$errorMsg[] .= "---Password is required.<br />";}//die($errorMsg.=
else if(strlen($password) < {$errorMsg[] = "---Invalid password. Min of 8 characters are required, max is 20.<br />";}//die(
else if(!$password2){$errorMsg[] .= "---Please repeat password.<br />";}//die($errorMsg.=
else if($user_captcha!=$captchacode_from_dbase){$errorMsg[] .= "---Please enter the correct CAPTCHA code.<br />";}//die($errorMsg.=
else if(!$accounttype){$errorMsg[] .= "---Please select account type or mode of transaction.<br />";}//die($errorMsg.=
else if(!$ip){$errorMsg[] .= "---Your location cannot be determined. You will not be allowed to continue the registration.<br />";}//die(
elseif($country=="Select your country"){$errorMsg[] .= "Invalid Country selection.<br />";}
else if($accounttype=="Select account type"){$errorMsg[] .= "Invalid Account Type selection.<br />";} //die($errorMsg);
else if($password!=$password2){$errorMsg[] .= "---Passwords did not match. Repeat correct passwords please.<br />";}//die(
else if($month > 12){$errorMsg[] .= "---Invalid month in date of birth.<br />";}//die(
else if($day > 31 || $day =="00"){$errorMsg[] .= "---Invalid day of birth.<br />";}//die(
else if($year < 1960 || $year > 1995 ){$errorMsg[] .= "---You are not qualified for this registration.<br />";}//die(
else {
//update the captcha table with a new generated captcha code
// this file regenerates the captcha code if it is less than 50.
require("update_captcha.php");
// Database duplicate Fields Check
$sql_username_check = mysql_query("SELECT id FROM members WHERE username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT id FROM members WHERE email='$email' LIMIT 1");
$username_check = mysql_num_rows($sql_username_check);
$email_check = mysql_num_rows($sql_email_check);
if($username_check > 0){
$errorMsg = "<u>ERROR:</u><br />Sorry, our system does not accept the username that you are using. Please try another.";
die($errorMsg); }
else if($email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Sorry, our system does not accept the email that you are using. Please try another.";
die($errorMsg);}
}
}//close the blocked if(isset)else {
// Add MD5 Hash to the password variable
$hashedPass = md5($password);
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO members (fullname,gender,dob,bio,ip,username,address,country,state,city,accounttype,email, password, signup_date)
VALUES('$fullname', '$gender','$dob','$bio','$ip','$username','$address','$country','$state','$city','$accounttype','$email','$hashedPass', now())
") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create a directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
//echo "Yes, " .$_POST['btn_register']. "<br />Username: ".$_POST['username']. "<br />Gender: ". $gender. "<br />Captcha: ". $user_captcha.
//"<br />Account Type: ".$accounttype. "<br />Address: ".$address;
//send the maill first before adding to the database
//compose email function here
$to = "$email";
$subject = "Complete your registration - Authentication Needed!";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $fullname . ',
<br /><br />
You have successfully created an account with us.<br /><br />
Your Login information is as follows: <br /><br />
Your E-mail Address: ' . $email . ' <br />
Your Password: ' . $password . ' <br /><br />
Your Account / Payment / Transaction information: '.$accounttype. '. <br /><br />
In order to be able to use your account, you must complete this step to activate your
account by clicking on the Account Activation Link below.
<br /><br />
Please click the link to activate now >>
<a href="http://localhost/easytimecorporation.com/lotto/activation.php?id=' . $id . '">
ACTIVATE NOW</a><br />
Thanks! <br /><br />
Eyo Honesty, <br />
Easytime Corporation.
</body>
</html>';
// end of message
//import the email settings file here.
include_once('emailSettings.php');
//check whether the mail is sent or not
if($mail->Send()) {
//echo "Message has been sent";
// Then print a message to the browser for the joiner
$message2 = '<br /> Hi ' . $fullname . ',
<br /><br />
You have successfully created an account with us. A mail has been sent to you at ' .$email. '.<br /><br />
In order to be able to use your account, please check your email and click on the Account Activation Link to activate your account.
<br /><br />
Thank you';
//echo $message2;
//update the captcha table with a new generated captcha code
// this file regenerates the captcha code if it is less than 50.
require("update_captcha.php");
//redirect user to success.php
$_SESSION['msg'] = $message2;
//echo $_SESSION['msg'];
//header("location: register_success.php?id=".$_SESSION['msg']);
} else {
//delete the user from database and delete the created user directory
require("delete_user.php");
rmdir("memberFiles/$id");//removes a created directory.
//update the captcha table with a new generated captcha code
// this file regenerates the captcha code if it is less than 50.
require("update_captcha.php");
die("Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo);
exit(); } // Exit so the form and page does not display, just this success message
}//close the $_POST[''] check line
?>
<!DOCTYPE html>
<HTML>
<HEAD>
<META CHARSET="UTF-8">
<LINK rel="icon" href="images/blueweb.ico" type="image/x-icon">
<LINK rel="stylesheet" href="style/style.css">
<TITLE>Users Registration Form.</TITLE>
<STYLE type="text/css">
<!--
.style1 {color: #FF0000}
.style3 {color: #FF0000; font-weight: bold; }
-->
</STYLE>
</HEAD>
<BODY>
<?php include_once("template_pageTop.php");?>
<DIV id="pageMiddle">
<FORM name="joinForm" id="joinForm" action="join_form.php" method="post" enctype="multipart/form-data">
<DIV align="center">
<H1>Users Registration Form</H1>
<HR style="width:80%; outline-style:groove; outline-color:#CCCCCC;">
<DIV align="justify">
<LEGEND style="margin-left:20px; "><STRONG><u>Basic Data</u></STRONG> Please all starred <SPAN class="style1">*</SPAN> fields in red colour are required.</LEGEND>
<BR />
<TABLE width="800" border="0" align="center" cellpadding="2" cellspacing="5" style="border-radius:10px; background-color:#006699; color:#FFFFFF;">
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Full Name:</STRONG></TD>
<TD width="520" align="left" valign="middle"><INPUT type="text" name="fullname" value="<?php if (isset($_POST['fullname'])) {echo "$fullname";} ?>" size="80" maxlength="40"></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Gender:</STRONG></TD>
<TD width="520" align="left" valign="middle">
<LABEL>
<INPUT type="radio" name="gender" value="male">
Male</LABEL>
<INPUT type="radio" name="gender" value="female">
Female</LABEL></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Date of Birth:</STRONG></TD>
<TD width="520" align="left" valign="middle">
Day <INPUT type="text" name="day" value="<?php if (isset($_POST['day'])) {echo "$day";} ?>" size="5" maxlength="2">
Month <INPUT type="text" name="month" value="<?php if (isset($_POST['month'])) {echo "$month";} ?>" size="5" maxlength="2">
Year <INPUT type="text" name="year" value="<?php if (isset($_POST['year'])) {echo "$year";} ?>" size="10" maxlength="4">
format --> DD / MM / YYYY </TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><STRONG><SPAN class="style1">*</SPAN> Tell us about yourself: </STRONG><EM>(min = 100 characters).</EM> </TD>
<TD width="520" align="left" valign="middle"><TEXTAREA name="bio" cols="50" rows="4"><?php if (isset($_POST['bio'])) {echo "$bio";} ?></TEXTAREA></TD>
</TR>
</TABLE>
</DIV>
<HR style="width:80%; outline-style:groove; outline-color:#CCCCCC;">
<DIV align="justify">
<LEGEND style="margin-left:20px; "><STRONG><u>Location Data</u></STRONG> Please all starred <SPAN class="style1">*</SPAN> fields in red colour are required.<BR>
</STRONG></u></LEGEND><BR />
<TABLE width="800" border="0" align="center" cellpadding="2" cellspacing="5" style="border-radius:10px; background-color:#006699; color:#FFFFFF;">
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Address 1 :</STRONG></TD>
<TD><INPUT name="address1" type="text" size="80" maxlength="70" value="<?php if (isset($_POST['address1'])) {echo "$address1";} ?>" /></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><STRONG>Address 2 (<EM>optional</EM>) :</STRONG></TD>
<TD><INPUT name="address2" type="text" size="80" maxlength="70" value="<?php if (isset($_POST['address2'])) {echo "$address2";} ?>" /></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> City:</STRONG></TD>
<TD><INPUT name="city" type="text" size="50" maxlength="30" value="<?php if (isset($_POST['city'])) {echo "$city";} ?>" /></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> State:</STRONG></TD>
<TD><INPUT name="state" type="text" size="50" maxlength="25" value="<?php if (isset($_POST['state'])) {echo "$state";} ?>" /></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Country :</STRONG></TD>
<TD><SELECT name="country">
<OPTION value="Select your country" selected="selected">Select your country</OPTION>
<OPTION value="Australia">Australia</OPTION>
<OPTION value="Canada">Canada</OPTION>
<OPTION value="Mexico">Mexico</OPTION>
<OPTION value="United Kingdom">United Kingdom</OPTION>
<OPTION value="United States">United States</OPTION>
<OPTION value="Zimbabwe">Zimbabwe</OPTION>
<OPTION value="Cameroon">Cameroon</OPTION>
<OPTION value="Nigeria">Nigeria</OPTION>
<OPTION value="South Africa">South Africa</OPTION>
<OPTION value="Uganda">Uganda</OPTION>
<OPTION value="Thailand">Thailand</OPTION>
<OPTION value="Brazil">Brazil</OPTION>
<OPTION value="China">China</OPTION>
<OPTION value="Japan">Japan</OPTION>
<OPTION value="Korea">Korea</OPTION>
<OPTION value="DR Congo">DR Congo</OPTION>
<OPTION value="Egypt">Egypt</OPTION>
<OPTION value="Germany">Germany</OPTION>
<OPTION value="Bene Republic">Bene Republic</OPTION>
<OPTION value="Niger">Niger</OPTION>
<OPTION value="Saudi Arabia">Saudi Arabia</OPTION>
<OPTION value="Italy">Italy</OPTION>
<OPTION value="France">France</OPTION>
<OPTION value="India">India</OPTION>
<OPTION value="Malaysia">Malaysia</OPTION>
<OPTION value="Spain">Spain</OPTION>
<OPTION value="Portugal">Portugal</OPTION>
<OPTION value="Port De Spain">Port De Spain</OPTION>
<OPTION value="Trinidad and Tobago">Trinidad and Tobago</OPTION>
<OPTION value="Chile">Chile</OPTION>
</SELECT></TD>
</TR>
</TABLE>
</DIV>
<HR style="width:80%; outline-style:groove; outline-color:#CCCCCC;">
<DIV align="justify">
<LEGEND style="margin-left:20px; "><U><STRONG>Login Information</STRONG></U> Please all starred <SPAN class="style1">*</SPAN> fields in red colour are required.</LEGEND>
<BR />
<TABLE width="800" border="0" align="center" cellpadding="2" cellspacing="5" style="border-radius:10px; background-color:#006699; color:#FFFFFF;">
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Username:</STRONG></TD>
<TD><DIV align="left">
<INPUT name="username" type="text" size="50" maxlength="25" value="<?php if (isset($_POST['username'])) { echo "$username";} ?>" />
</DIV></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><P><SPAN class="style3">*</SPAN><STRONG> Password: </STRONG></P>
<P><EM>(8 characters minimum, 20 max.) </EM></P></TD>
<TD><DIV align="left">
<INPUT name="password" type="password" value="<?php if (isset($_POST['password'])) {echo "$password";} ?>" size="40" maxlength="20">
No spaces or special charactes. </DIV></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><P><SPAN class="style3">*</SPAN><STRONG> Repeat Password</STRONG></P>
<P><EM>(8 characters minimum, 20 max.) </EM></P></TD>
<TD><DIV align="left">
<INPUT name="password2" type="password" value="<?php if (isset($_POST['password2'])) {echo "$password2";} ?>" size="40" maxlength="25">
No spaces or special charactes. </DIV></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> E-mail Address</STRONG></TD>
<TD><DIV align="left">
<INPUT name="email" type="text" value="<?php if (isset($_POST['email'])) {echo "$email";} ?>" size="80" maxlength="45" />
</DIV></TD>
</TR>
</TABLE>
</DIV>
<HR style="width:80%; outline-style:groove; outline-color:#CCCCCC;">
<DIV align="justify">
<LEGEND style="margin-left:20px; "><U><STRONG>Account / Payment Information</STRONG></U> Please all starred <SPAN class="style1">*</SPAN> fields in red colour are required.</LEGEND> <BR />
<TABLE width="800" border="0" align="center" cellpadding="2" cellspacing="5" style="border-radius:10px; background-color:#006699; color:#FFFFFF;">
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> Account Type:</STRONG></TD>
<TD><SELECT name="accounttype" size="3">
<OPTION value="Select account type">Select Account Type</OPTION>
<OPTION value="atm">ATM</OPTION>
<OPTION value="ecurrency" selected>e-Currency</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD align="right" valign="middle" style="width:260px; height:40px; padding:15px;"><SPAN class="style3">*</SPAN><STRONG> CAPTCHA:</STRONG></TD>
<TD>
<INPUT name="captchacode" type="text" value="<?php if (isset($_POST['captchacode'])) {echo "$user_captcha";}?>" maxlength="12">
<?php echo "<font style ='color:#ffff00; font-size:20px; font-weight:bolder; font:Broadway;'> $captchacode_from_dbase</FONT> <font style='color:#ffffff;'-->Enter the code shown.</FONT>";?> </TD>
</TR>
</TABLE>
</DIV>
<HR style="width:80%; outline-style:groove; outline-color:#CCCCCC;">
<DIV align="left" style="margin-left:0px;">
<DIV align="center">
<INPUT name="btn_register" type="submit" value="R E G I S T E R">
<INPUT name="reset" type="reset" value="R E S E T" style="font:Georgia, 'Times New Roman', Times, serif; font-size:14px;">
<BR /><BR />
</DIV>
</DIV>
<?php
if (empty($errorMsg)=== false) {
echo '<ul>';
foreach($errorMsg as $error) {
echo '<li>', $error, '</li>';
}
echo '</ul>';
}
?>
</FORM>
</DIV>
<?php include_once("template_pageBottom.php");?>
</BODY>
</HTML>
join_form.php