无法使用 JSON 编码获取后端数组数据

问题描述

我有一个脚本 (Users.php),它使用 JSON_encode 在 HTML 表中显示对象数组。

如:

Users.php :

html //空表

script //使用json encode填充表格,获取php数组并显示表格中的内容

myPhp.php :

从数据库获取信息并创建数组。

我的 php 文件运行良好,我的脚本也是如此。唯一的问题是当我使用 JSON_encode 将数组从 php 获取到脚本时,它显示错误 : Uncaught SyntaxError: Unexpected token '

我的 Users.php:

 <body >
    <!-- adding user -->
    <form  class ="formArea" id = "addUser" action = "addUsers.php" method="POST">
      <!-- addUsers.php will add users to the database then display Users.php again -->
    </form>
    <!-- display users -->
        <table id="usersTable">
            <!-- table to display user info -->
        </table>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <!--jquery-->
    <script>

        //display all users in table
        var users = <?php echo JSON_encode($rows); ?>
        
        displayUsers(users);
        function displayUsers(users){
            for(i = 0; i<users.length; i++)
            {
               //create table row
               //add user information to the row
            }
        }     
    </script>
    
</body>

我的 myPhp.php:

    <?php
// fur UI Users.php
// calls all users from the db and displays them in users table
$sql = new mysqli("localhost","root","","atds");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
 }
 
 $query = "SELECT * FROM users";
 $result = $sql->query($query);           
 while($row = $result->fetch_object())
 {
     $rows[]=$row;
 }

 // Close connection
 $result->close();
 $sql->close();

?>

我尝试过的:

我在使用 json_encode 之前尝试包含 php 文件

<?php include'myPhp.php' ?>
var users = <?php echo json_encode($rows); ?> 

这在我运行 Users.php 时有效,但是如果我添加一个用户(通过在此网页中提交表单),添加用户文件在添加用户后再次读取 Users.php,用户最终不会显示,我会有同样的错误: 未捕获的语法错误:意外的标记“

是否有其他使用 JSON_encode 的方法不会导致此错误?

解决方法

我没有收到与您相同的错误,但 MyUsers.php 不知道您尝试使用的变量:



    //display all users
    //users array contains names and roles
    var users = <br />
<b>Warning</b>:  Undefined variable $rows in <b>/var/www/html/public/temp/myUsers.php</b> on line <b>17</b><br />
null
        displayUsers(users);
    function displayUsers(users){
        for(i = 0; i<users.length; i++)
        {
            //create table row
            //add user information to the row
        }
    }

有几种不同的方法可以将 $rows 放入 myUsers.php。

学习时更容易理解的一种方法是在一页中完成所有事情:在顶部获取数据并在底部呈现 HTML。

MyUser2.php:我没有设置数据库并对行进行硬编码。假设您的数据库设置有效,您可以删除评论和硬编码用户。请注意,由于您正在输出一个有效的 JSON 数组,因此您不必在 javascript 中重新解析它。

它在浏览器中看起来像这样:


    //display all users
    //users array contains names and roles
    var users = ["user1","user2","user3"];

        displayUsers(users);
    function displayUsers(users){
        for(i = 0; i<users.length; i++)
        {
            //create table row
            //add user information to the row
            document.write(users[i] + '<br>');
        }
    }

MyUser2.php 文件列表:

<?php
/*
// fur UI Users.php
// calls all users from the db and displays them in users table
$sql = new mysqli("localhost","root","","atds");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
    exit();
}

$query = "SELECT * FROM users";
$result = $sql->query($query);
while($row = $result->fetch_object())
{
    $rows[]=$row;
}

// Close connection
$result->close();
$sql->close();
*/
$rows[] = 'user1';
$rows[] = 'user2';
$rows[] = 'user3';

?>
<body >
<!-- adding user -->
<form  class ="formArea" id = "addUser" action = "addUsers.php" method="POST">
    <!-- addUsers.php will add users to the database then display Users.php again -->
</form>
<!-- display users -->
<table id="usersTable">
    <!-- table to display user info -->
</table>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <!--jquery-->
<script>

    //display all users
    //users array contains names and roles
    var users = <?php echo JSON_encode($rows); ?>;

        displayUsers(users);
    function displayUsers(users){
        for(i = 0; i<users.length; i++)
        {
            //create table row
            //add user information to the row
            document.write(users[i] + '<br>');
        }
    }
</script>

</body>

这会让你开始。学习 PHP 的一个很好的框架是 CodeIgniter。 Laravel 是一个很棒的框架并且更受欢迎,但它添加了许多神奇的东西,使普通 PHP 更难学习。

,

在您的“Users.php”中

你应该首先包含“myPhp.php”

   <?php include'myPhp.php' ?>

并像这样使用它:

var json = '<?php echo JSON_encode($rows); ?>';
var users = JSON.parse(json);
,

解决了。 当我运行 Users.php 时,该表显示正确,但在通过执行包含 readfile('Users.php') 的 AddUsers.php 添加用户时显示该错误。 问题实际上出在 readfile(Users.php)addUsers.php 中。

readfile 不执行 Users.php 而是只显示其中的元素。因此 JSON_encode 从未被执行,Users.php 也不知道用户。

我通过将 addUsers 中的 readfile('Users.php') 更改为 include 'Users.php';

解决了这个问题

Users.php 代码变成了:

<?php include 'displayUsers.php'; ?>;
        var users = <?php echo JSON_encode($rows); ?>;
        
        displayUsers(users);
        function displayUsers(users){...}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...