我已经建立了一个注册表单页面。它允许您注册帐户并将其存储在数据库中。
最初,它将任何错误发回到同一页面(称为index.php)
然而,在进入注册页面之前,我想向用户展示一个"我已经拥有一个帐户"按钮和"创建新帐户"按钮。这是因为该网站适合儿童,我不想在同一页面上加载大量文字。
所以,如果你去这里,你会发现有两种选择。这些通过AJAX以相关的形式加载到div login-register-wrapper中。单击一个,它将加载register-form.php,另一个是login-form.php(当前不工作)
http://marmiteontoast.co.uk/fyp/login-register/index.php
我遇到的问题是,在验证时,它会回发到index.php
header('Location: index.php');
因此,如果您没有输入用户名,而不是发布错误,则会将您带回第一页...
如何使用AJAX使其能够在register-form.php文件中发布?
这是完整的注册页面代码:
<?php
session_start();
require 'functions.php';
if(isset($_POST['sign-up'])){
// username
if (isset($_POST['username'])){
$username = mysql_real_escape_string(trim($_POST['username']));
$_SESSION['status']['register']['username'] = $username;
if(strlen($username) > 3){
if(strlen($username) < 31){
if(user_exists($username) === true){
$_SESSION['status']['register']['error'][] = 'That username is already taken. Sorry, please try again with a different username.';
} else{
// passed
// continue
}
} else {
$_SESSION['status']['register']['error'][] = 'The username is greater than 30 characters.';
}
} else {
$_SESSION['status']['register']['error'][] = 'The username is less than 3 characters.';
}
} else {
$_SESSION['status']['register']['error'][] = 'The username is not entered.';
}
if (isset($_POST['password'])){
$password = mysql_real_escape_string(trim($_POST['password']));
if(strlen($password) >= 8){
$password = hash_function($password);
} else {
$_SESSION['status']['register']['error'][] = "Your secret password is too short. You should make a password with at least 8 letters.";
}
} else {
$_SESSION['status']['register']['error'][] = "You haven't put in a password.";
}
// Email address
if (!empty($_POST['email_address'])){
$email_address = mysql_real_escape_string(trim($_POST['email_address']));
$_SESSION['status']['register']['email_address'] = $email_address;
if(strlen($email_address) > 10){ // email address less than 10
if(strlen($email_address) < 161){ // if longer than 160
if(email_valid($email_address) == false){ // email address invalid format
$_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
}
else{
// passed min length, passed max length, passed validation
// Continue
}
}
else
{
$_SESSION['status']['register']['error'][] = 'The email address is too long.';
}
}
else
{
$_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
}
}
else{// passed (no email input)
}
if (isset($_POST['tos'])){
$_SESSION['status']['register']['tos'] = $_POST['tos'];
if(empty($_SESSION['status']['register']['error'])){
if(register($email_address, $username, $password) === true){
// Success!!
$_SESSION['status']['register']['success'] = true;
// Sends an email
send_email($email_address);
} else {
echo mysql_error();
die();
$_SESSION['status']['register']['error'][] = "Something went wrong. We're sorry. Please try again.";
}
} else {}
} else {
$_SESSION['status']['register']['error'][] = "You have to agree to the House Rules to be able to sign up.";
}
header('Location: index.php');
} else {
header('Location: index.php');
}
?>
答案 0 :(得分:0)
如果主页由AJAX请求产生,则标题无法重定向主页面。相反,您可以使用AJAX回调来解释页面结果并相应地重定向页面。干净的方法是始终返回JSON结果。然后,这可能包含类似redirect
的字段,其字段对应于用户应重定向到的页面。
您可以使用json_encode将PHP数组转换为JSON结果。如果你正在使用jQuery,parseJSON是一个很好的解析JSON代码的函数。检索到redirect
值后,您可以使用location.window = redirectUrl;
重定向用户。