I have been struggling to figure this out of the past couple days. In fact, as I am new to all of this it seems I am constantly trying to figure something out. Anyway, on to my question.
I am trying to encode an array in PHP and retrieve it in JQuery using JSON using the following code:
Included function from 'core/init.php':
function output_errors($errors) {
echo json_encode($errors);
}
login.php:
<?php
include 'core/init.php';
logged_in_redirect();
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'Please enter a username and password.';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your account.';
} else {
$login = login($username, $password);
if ($login === false) {
$errors[] = 'That username/password combination is incorrect.';
}
else {
//start session
$_SESSION['user_id'] = $login;
//redirect to home.
header('Location: index.php');
exit();
}
}
} else {
header('Location: index.php');
}
include 'includes/overall/header.php';
if (empty($errors) === false) {
?>
<h2>We tried to log you in but...</h2>
<?php
output_errors($errors);
}
include 'includes/overall/footer.php';
?>
JQuery:
function logErrors(errors) {
alert(errors);
};
$(document).ready(function() {
$.getJSON('login.php', function(errors){
logErrors(errors);
});
});
I am working locally and have gotten it to work a test file:
test.php:
<?php
function output_errors($errors) {
echo json_encode($errors);
}
output_errors($errors);
?>
In some of my research I have found that the version of PHP may be an issue. That doesn't seem to be my problem as I have gotten it to work with another file.
Another common problem others seem to have is the fact that $.getJSON is asynchronous and may be running before php has a chance to return the array. I'm not sure if my JQuery is structured properly or not to avoid that issue.
What I keep getting is no alert and the error echoed like this: ["We can't find that username. Have you registered?"].
How do I get my JQuery to retrieve the encoded array and trigger the function logErrors?
Thanks in advance for any help you guys can offer. Sorry if I posted too much code or not enough...rookie mistake.
UPDATE
Here are the user_exists and user_active functions:
user_exists:
function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysql_result($query,0) == 1) ? true : false;
}
user_active:
function user_active($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'AND `active` = 1");
return (mysql_result($query,0) == 1) ? true : false;
}
E
$.getJSON('login.php', function(errors){ logErrors(errors); });ought to alert the entire page.