I tried consolidating code and apparently my ajax login stopped working. When I disable JS, it works perfectly. It reads the PHP and gives appropriate errors, etc. It just doesn’t seem to send the JSON correctly.
The HTML form goes to process-home.php. Below is this file.
<?php
session_start();
require_once($_SERVER["DOCUMENT_ROOT"]."/cadeui/system/includes/bootstrap.php");
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(isset($_POST["login"]))
{
$email=filter_input(INPUT_POST,"email");
$password=filter_input(INPUT_POST,"password");
$remember=filter_input(INPUT_POST,"remember");
$formData=new UserServices($pdo);
$isValid=$formData->checkCredentials($email,$password,$remember);
}
else if(isset($_POST["subscribe"]))
{
$email=filter_input(INPUT_POST,"email");
$formData=new UserServices($pdo);
$isValid=$formData->newSubscriber($email);
}
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
if($isAjax)
{
$result=["result" => $isValid];
header("Content-Type: application/json");
exit(json_encode($result));
}
if(isset($_POST["login"]))
{
if($isValid[0])
header("Location: http://www.codefundamentals.com/cadeui/dashboard");
else
header("Location: http://www.codefundamentals.com/cadeui/login?error=$isValid[1]");
}
else if(isset($_POST["subscribe"]))
{
if($isValid[0])
header("Location: http://www.codefundamentals.com/cadeui/index#newsletter#newsletter?result=success");
else
header("Location: http://www.codefundamentals.com/cadeui/index#newsletter?error=$isValid[1]");
}
}
?>
Where it checks for AJAX, that part seems to not be doing its job. If JS is enabled, it uses AJAX to call process-home.js. File below.
This AJAX
$.ajax({
type: "POST",
dataType: "text",
data: {
'email': user,
'password':pass,
'remember':remember
},
url: "/cadeui/system/includes/process-home",
success: function(data) {
data = JSON.parse(data);alert(data.result);
if(data.result[0])
window.location.href="http://www.codefundamentals.com/cadeui/dashboard";
else
{
$(".overlay .loading-bar").remove();
$(".overlay").css("z-index","9997");
$("#submit, #email, #password").prop("disabled", false);
if(data.result[1]==="userpass")
{
$("<span id=\"userpass-error\" class=\"error\">Error: Your username or password is incorrect. Please try again.</span>").prependTo("fieldset");
}
else if(data.result[1]==="attempts")
{
$("<span id=\"attempts-error\" class=\"error\">Error: You have exceeded the maximum number of attempts. Please try again later.</span>").prependTo("fieldset");
}
$("#password, #email").removeClass("valid");
}
}
Again, this works fine in the PHP version, but not the AJAX version. I alert data.result[0] and I get null. Not sure why it’s just appearing now. I tried looking at past versions but I don’t see what’s different. Can anyone see why this stopped working?
http://www.codefundamentals.com/cadeui/index