将JSON数组解码为Javascript

时间:2016-11-27 19:49:47

标签: javascript php arrays json

我正在为我的网站上的新帐户设置激活队列。系统将通过迭代数组,每个项目作为另一个包含新用户帐户详细信息的数组,然后将数组的每个项目显示给用户,以便管理员可以接受或拒绝该帐户。我已经在PHP文件中收集了数组的所有信息,但是我需要将第一个帐户详细信息返回到Javascript以显示它们。我目前尝试的代码不起作用,但在Chrome的开发者控制台或我的服务器目录中没有给出任何错误。如果有人可以在我的代码上扫视他们的眼睛会很好,因为我知道我的运气会很明显,或者因为我不完全理解JSON。提前谢谢。

<?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

2 个答案:

答案 0 :(得分:1)

这应该有效:(你用单引号包围变量,错误类型forename(在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]]);
}
?>

答案 1 :(得分:0)

<?php
session_start();

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

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

if ($results = TF_Core::$MySQLi->DB->query($ActQueueQuery)) {
    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(['Username'=>$_SESSION["ActQueue"][0][0], 'Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'Joined'=>$_SESSION["ActQueue"][0][3]]);
    }
}
?>

看起来像是一些奇怪的古怪行为,但我设法让它在我的一位德国朋友的帮助下工作。