1

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

7
  • Might need to do print_r() instead of echo; might need to have a header with content-type explicitly set to a javascript object. I can't tell which piece of code is index.php. Could you label them better? Commented Mar 10, 2013 at 3:57
  • I ran it with print_r($errors) and got this: Array ( [0] => We can't find that username. Have you registered? ), in place of ["We can't find that username. Have you registered?"]. index.php isn't up there the php is from a login page I will try to update the labels to make it clearer. Also, adding the header doesn't seem to work either. Commented Mar 10, 2013 at 4:42
  • I don't understand what you're trying to do. Are you trying to alert an entire HTML page? $.getJSON('login.php', function(errors){ logErrors(errors); }); ought to alert the entire page. Commented Mar 10, 2013 at 5:15
  • where are the user_exists and user_active functions? Commented Mar 10, 2013 at 5:20
  • Think about what you're doing. You're asking jQuery to fetch JSON, but your PHP script outputs JSON surrounded by HTML. Do you expect jQuery to automagically figure out where JSON starts and ends? Commented Mar 10, 2013 at 6:08

2 Answers 2

1

Your problem may be needing to set the header before returning the json object

function output_errors($errors) {
    header('Content-Type: application/json');
    echo json_encode($errors);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Still doesn't seem to be working. I gave it a shot earlier as I also came across that in my search for a solution. I"m not sure if this helps either but the test file doesn't seem to need it to work.
0

Finally, figured it out. bozdoz and DCoder turned out to be right. I did not realize that php would echo the entire page (rookie mistake). I was under the impression that it would only echo the json_encoded array.

For anyone running into a similar issue, make sure you are working with a file that only echos the desired result.

Thanks for the help guys and gals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.