我是php新手,我创建了一个用户注册表单。但每当我提交表单时,我的部分页面都不会加载。我看到“成功注册”消息,但我没有看到我的右侧边栏,页脚或我的包装div。在整天搜索互联网后,我得出结论是exit()函数。但是,如果我离开退出功能并提交表单,我的所有页面都会加载,但原始注册表单仍显示在我的“成功注册”消息下方。有人可以帮忙吗?
<?php
require_once('_includes/connectvars.php');
if (isset($_POST['submitted'])) {
$firstname = mysql_real_escape_string(trim($_POST['firstname']));
$lastname = mysql_real_escape_string(trim($_POST['lastname']));
$email = mysql_real_escape_string(trim($_POST['email']));
$birthdate = mysql_real_escape_string(trim($_POST['birthdate']));
$zipcode = mysql_real_escape_string(trim($_POST['zipcode']));
$gender = $_POST['gender'];
$username = mysql_real_escape_string(trim($_POST['username']));
$password1 = mysql_real_escape_string(trim($_POST['password1']));
$password2 = mysql_real_escape_string(trim($_POST['password2']));
$terms = $_POST['terms'];
$newsletter = $_POST['newsletter'];
$activationkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$captchchk = 1;
require_once('_includes/recaptchalib.php');
$privatekey = "some#";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (preg_match ('%^[A-Za-z\.\' \-]{2,15}$%', stripslashes(trim($_POST['firstname'])))) {
$firstname = escape_data($_POST['firstname']);
} else {
$firstname = FALSE;
echo '<p class="error">Please enter a valid first name!</p>';
}
if (preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', stripslashes(trim($_POST['lastname'])))) {
$lastname = escape_data($_POST['lastname']);
} else {
$lastname = FALSE;
echo '<p class="error">Please enter a valid last name!</p>';
}
if (preg_match('%^(0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/](19|20)?[0-9]{2}$%', stripslashes(trim($_POST['birthdate'])))) {
$birthdate = escape_data($_POST['birthdate']);
} else {
$birthdate = FALSE;
echo '<p class="error">Please enter a valid date of birth in the format of MM/DD/YYYY</p>';
}
if (preg_match ('%^[0-9]{5}$%', stripslashes(trim($_POST['zipcode'])))) {
$zipcode = escape_data($_POST['zipcode']);
} else {
$zipcode = FALSE;
echo '<p class="error">Please enter a valid 5 digit zip code!</p>';
}
if ($gender !== 'M') {
if ($gender !== 'F') {
echo '<p class="error">Please select your gender!</p>';
}
}
if (preg_match ('%^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*(\S{4,75})$%', stripslashes(trim($_POST['username'])))) {
$username = escape_data($_POST['username']);
} else {
$username = FALSE;
echo '<p class="error">Please enter a valid username!</p>';
}
if (preg_match ('%^[A-Za-z0-9]{6,15}$%', stripslashes(trim($_POST['password1'])))) {
$password1 = escape_data($_POST['password1']);
if (($_POST['password1'] == $_POST['password2']) && ($_POST['password1'] != $_POST['username'])) {
$password1 = escape_data($_POST['password1']);
} elseif ($_POST['password1'] == $_POST['username']) {
$password1 = FALSE;
echo '<p class="error">Your password cannot be the same as the username!</p>';
} else {
$password1 = FALSE;
echo '<p class="error">Your password did not match the confirmed password!</p>';
}
} else {
$password1 = FALSE;
echo '<p class="error">Please enter a valid password!</p>';
}
if ($terms !== 'Y') {
echo '<p class="error">You must agree to the terms of use!</p>';
}
if (!$resp->is_valid) {
echo '<p class="error">The CAPTCHA Code wasn\'t entered correctly!</p>';
$captchchk = 0;
}
if ($firstname && $lastname && $email && $birthdate && $zipcode && $gender && $username && $password1 &&
$terms && $captchchk) {
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if(mysql_num_rows($result) == 1) {
echo '<br><br><p class="error">An account already exists for this username. Please select a different
username.</p>';
$username = "";
} else {
if(mysql_num_rows($result) == 0) {
$query = "INSERT INTO members (first_name, last_name, email, dob, zip_code, gender, username, password, join_date, newsletter, active, terms, status) VALUES ('$firstname', '$lastname', '$email', STR_TO_DATE('$birthdate', '%m/%d/%Y'), '$zipcode', '$gender', '$username', SHA('$password1'), NOW(), '$newsletter', '$activationkey', '$terms', 'verify')";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if (mysql_affected_rows() == 1) {
$to = $_POST['email'];
$subject = "Registration";
$message = "Welcome to Mysite.com!\n\nYou, or someone using your email address, has completed registration at Mysite.com. You can complete registration by clicking the following link:\n\nhttp://www.mysite.com/verify.php?$activationkey\n\nIf this is an error, ignore this email and you will be removed from our mailing list.\n\nRegards,\n\nThe Mysite.com Team";
$headers = 'From: noreply@ Mysite.com' . "\r\n" .
'Reply-To: noreply@ Mysite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo '<br /><br /><p>Thank you for registering! A confirmation email has been sent to your email. Please click on the link in that email in order to activate your account.</p>';
} else {
$queryString = $_SERVER['QUERY_STRING'];
$query = "SELECT * FROM members";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
while($row = mysql_fetch_array($result)){
if ($queryString == $row["active"]){
echo "Congratulations!" . $row["username"] . " is now a member of Mysite.com!";
$query="UPDATE members SET active = '', status='activated' WHERE (user_id =
$row[user_id])";
if (!mysql_query($query)) {
die('Error: ' . mysql_error());
}
}
}
}
}
}
}
exit();
mysql_close();
}
答案 0 :(得分:1)
您不需要exit()
,只需if ... else
块:
if ((/* form submitted */) && (/* everything is OK */))
{
// do stuff
}
else
{
// show form
}
答案 1 :(得分:1)
有两种解决方案。