Currently the success of my PHP if statement just reloads the page:
header('Location: index.php');
Added: link to the site here http://marmiteontoast.co.uk/fyp/login-register/index.php
However I want to run a JavaScript basic AJAX request instead to load a page register-success.php into the div:
function loadSuccess()
{var xmlhttp;
if (window.XMLHttpRequest)
{xmlhttp=new XMLHttpRequest();}
else
{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function()
{if (xmlhttp.readyState==4 && xmlhttp.status==200)
{document.getElementById("login-register-wrapper").innerHTML=xmlhttp.responseText;}}
xmlhttp.open("GET","register-success.php",true);
xmlhttp.send();}
However I am unsure about this combination of JavaScript and PHP and how to implement it
1) Is there some sort of PHP function I can wrap around a piece of JS to make it enact it?
2) Is there a way to do a simple AJAX call like this in PHP instead? Or something that mimics it?
Here is the function that validates my form. If the form validates, I need to reload with AJAX and call in register-success file
<?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 {
// success script with AJAX goes here
}
?>
login-register-wrapper