Hi,
I am looking to be able to display errors from my forms or whether the email address is already taken on the same page. My script is shown below:
if(isset($_POST['subregister'])) {
require_once('../sign-up/recaptchalib.php');
$privatekey = "Hidden";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
require_once(MySQL);
$trimmed = array_map('trim', $_POST);
$errors = array();
if (preg_match ('/^[a-zA-Z0-9 \' . - ]{2,20}$/i', $trimmed['username'])) {
$un = mysqli_real_escape_string ($dbc, $trimmed['username']);
} else {
$errors[] = 'Please Choose A Username';
}
if (preg_match ('/^[a-zA-Z0-9\' ]{2,20}$/',$trimmed['password']) ) {
if ($trimmed['password'] == $trimmed['password1']) {
$p = mysqli_real_escape_string ($dbc, $trimmed['password']);
} else {
$errors[] = 'Your Password Did Not Match The Confirmed Password';
}
} else {
$errors[] = 'Please Enter A Valid Password';
}
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
$e = mysqli_real_escape_string ($dbc, $trimmed['email']);
} else {
$errors[] = 'Please Enter A Valid Email Address';
}
if (isset($_POST['terms']) && ($_POST['terms'] == 'Yes')) {
$t = mysqli_real_escape_string($dbc, $trimmed['terms']);
} else {
$errors [] = 'Please Accept Hidden Terms and Conditions';
}
$user_IP = $_SERVER['REMOTE_ADDR'];
$User_Status = 'Offline';
$user_Level = 0;
if (!$errors) {
$q = "SELECT User_ID FROM Members WHERE Email='$e'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) == 0) {
$a = md5(uniqid(rand(), true));
$q = "INSERT INTO Members(Username,Password,Email,Activate,Terms,User_Registered,User_IP,User_Last_Login,User_Status,User_Level) VALUES ('$un',SHA('$p'),'$e','$a','$t',NOW(),'$user_IP',NOW(),'$User_Status','$user_Level')";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) {
$message = '<!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" xml:long="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Registration Details</title>
<style type="text/css">
html{ height: 100%; width:100%;}
body{font-family:Tahoma,Trebuchet MS,Lucida Grande,sans-serif;line-height:1.3em;min-height:100%;}
h2{font-size: 18px;font-weight:400;color:#404141;letter-spacing: 1px;padding:15px 18px 10px 18px;}
p.info{line-height:1.3em;font-size: 12px;color:#000;padding:0px 18px 0px 18px;}
</style>
</head>
<body>';
$message .="<br />
<h2>REGISTRATION CONFIRMATION</h2>
<p class=\"info\">
This is to confirm that you have signed up to Hidden, please follow the steps below to activate your
account:<br /><br />";
$message .= "<a href=\"" . BASE_URL .'sign-up/activate.php?x=' . urlencode($e) . "&y=$a\" />Activate Your Account</a>";
$message .= "</p>
<h2>ACTIVATE ANOTHER WAY</h2>
<p class=\"info\">
If the above Activation Link does not work please copy and paste the code below into your browser address bar
to activate your account:<br /><br />";
$message .= BASE_URL.'sign-up/activate.php?x='.urlencode($e)."&y=$a";
$message .="</p>
</body>
</html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .='From: hidden Registration <noreply@hidden>' . "\r\n" .
'Reply-To: Admin@hidden'. "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($trimmed['email'],'Registration Confirmation',$message,$headers);
echo '<div id="rows">
<div id="container">
<div id="registerPanel>
<div class="goodResult">
<h2>Congratulations</h2>
<div class="resultRow">
<p>
Thank you for registering! A confirmation email has been sent to your email address. Please click on the link
in that email in order to activate your account.
</p>
</div>
</div>
</div>
</div>
</div>';
include ('../includes/footer.php');
exit();
} else {
echo '<div id="registerPanel">
<div class="badResult">
<h2>REGISTER ERROR</h2>
<div class="resultRow">
<p>
You could not be registered due to a system error, We apologize for any inconvenience.
</p>
</div>
</div>
</div>';
}
} else {
echo '<div id="registerPanel">
<div class="badResult">
<h2>EMAIL ALREADY REGISTERED</h2>
<p>
That email address has already been registered. If you have forgotten your password, use the link at the login page to
have your password sent to you.
</p>
</div>
</div>';
}
} else {
echo '<div id="rows">
<div id="container">
<div id="registerPanel">
<div class="badResult">
<h2>FORM VALIDATION ERRORS</h2>
<p>
Please correct the following errors:<ul>';
foreach ($errors as $v) {
echo "<li>$v.</li>\n";
}
echo '</ul>
</p>
</div>
</div>
</div>
</div>';
}
mysqli_close($dbc);
} //END OF SUCCESS FOR CAPTURE
} //END OF SUBREGISTER
?>
<div id="rows">
<div id="container">
<div id="registerPanel">
<h1>Registration</h1>
<form name="register" id="register" method="post" action="http://www.Hidden/sign-up/register.php">
<div id="bx1">
<h2>User Details</h2>
<div class="rowElem">
<label for="username">Username:</label>
<input type="text" name="username" size="20" maxlength="20" class="username" value="<?php if (isset($trimmed['username'])) echo $trimmed['username']; ?>" />
</div>
<div class="rowElem">
<label for="password">Password:</label>
<input type="password" name="password" size="20" maxlength="20" class="psw" value="<?php if (isset($trimmed['password'])) echo $trimmed['password']; ?>" />
</div>
<div class="rowElem">
<label for="password">Confirm Password:</label>
<input type="password" name="password1" size="20" maxlength="20" class="psw" value="<?php if (isset($trimmed['password1'])) echo $trimmed['password1']; ?>" />
</div>
<div class="rowElem">
<label for="email">E-Mail:</label>
<input type="text" name="email" size="80" maxlength="80" class="email" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" />
</div>
</div>
<div id="bx1">
<h2>Terms & Conditions</h2>
<div class="rowElem">
<label for="terms" class="l2">You agree to the <a href="#">Terms & Conditions</a> set out by Hidden</label>
<input type="radio" name="terms" class="terms" value="Yes" />
</div>
</div>
<div id="bx1">
<div class="rowElem">
<h2>Security Question</h2>
<div class="capture">
<?php
require_once('../sign-up/recaptchalib.php');
$publickey = "Hidden"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</div>
</div>
</div>
<div class="rowElem">
<input type="submit" class="regBut" name="register" value="Register" />
<input type="hidden" name="subregister" value="TRUE" />
</div>
</form>
</div>
I would consider AJAX as a way of doing it but using the POST method but I did have problems when trying to implement it with the checking of regular expressions and also I don't really understand AJAX well enough to dive into head first.
Any help would be much appreciated.
Thanks