Thomas Smyth Thomas Smyth - 5 days ago 6
JSON Question

Decode JSON array into Javascript

I'm working on an Activation Queue for new accounts on my website. The system will work by iterating through an array with each item as another array containing new user's account details in and then display each item of the array to the user so the Admin can either accept or deny the account. I have collected all the information for the array in a PHP file, however I need to get the first account details back the to Javascript to display them. The code I'm trying currently just doesn't work, but no error is given either in Chrome's dev console or in my server's directory. It would be nice if someone could scan their eyes over my code as knowing my luck it will be something so obvious or it's because I don't understand JSON fully. Thanks in advance.

<?php
session_start();

require "classes.php";
$TF = new TF_Core ();

$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";

if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
$statement->execute();
$results = $statement->get_result();
}

if($results->num_rows == 0){
$data = 1;
echo $data;
}
else{
$_SESSION["ActQueue"] = array("");

while($row = $results->fetch_assoc()){
array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forname"], $row["joined"]));
}
$data = 0;
echo json_encode(['Surname'=>'$_SESSION["ActQueue"][1][0]','Forename'=>'$_SESSION["ActQueue"][0][1]','Username'=>'$_SESSION["ActQueue"][0][2]','Joined'=>'$_SESSION["ActQueue"][0][3]']);
}
?>


Javascript:

function ActQueueRefresh(){
$.post('../functions/php/fncactqueuerefresh.php', {}, function(data) {
var returnValue = JSON.parse(data);
if (data == 0){
$('#divQueueError').html('');
$("#spnSurname").text(returnValue['Surname']);
$("#spnForename").text(returnValue['Forename']);
$("#spnUsername").text(returnValue['Username']);
$("#spnJoined").text(returnValue['Joined']);
$('#divQueueError').text('');
}
else if (data == 1){
$("#spnSurname").text('');
$("#spnForename").text('');
$("#spnUsername").text('');
$("#spnJoined").text('');
$('#divQueueError').html('<p class="text-center text-danger bg-danger" id="pUPInc">There are no accounts pending activation!</p>');
}
});
}


http://thomas-smyth.co.uk/admin/activationqueue.php

Answer

That should work: ( You surrounded variables with single quotes, mistype forename ( before forname ) )

<?php
session_start();

require "classes.php";
$TF = new TF_Core ();

$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";

if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
    $statement->execute();
    $results = $statement->get_result();
}

if($results->num_rows == 0){
    $data = 1;
    echo $data;
}
else{
    $_SESSION["ActQueue"] = array();

    while($row = $results->fetch_assoc()){
        array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forename"], $row["joined"]));
    }
    $data = 0;
    echo json_encode(['Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'Username'=>$_SESSION["ActQueue"][0][0],'Joined'=>$_SESSION["ActQueue"][0][3]]);
}
?>
Comments