【问题标题】:unable to get back end array data using JSON-encode无法使用 JSON 编码获取后端数组数据
【发布时间】:2021-04-09 22:23:15
【问题描述】:

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

如:

Users.php:

html //空表

脚本//使用json编码填充表格,获取php数组并显示表格中的内容

myPhp.php:

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

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

我的用户.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?​​p>

【问题讨论】:

  • 忽略上面的评论,我认为你需要使用JSON.parse(users)解析你的json数据,因为json_encode返回一个字符串而不是实际的json对象。
  • 我尝试在 users= JSON_encode() 之后添加 JSON_ parse(users)。我仍然遇到同样的错误。我不明白如果 encode 返回一个字符串,那么当我包含 myPhp.php 时,我的 Users.php 是如何工作的

标签: javascript php json backend jsonencoder


【解决方案1】:

我没有收到与您相同的错误,但 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:我没有设置数据库并对行进行硬编码。假设您的数据库设置有效,您可以删除 cmets 和硬编码用户。请注意,由于您输出的是有效的 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 更难学习。

【讨论】:

    【解决方案2】:

    在您的“Users.php”中

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

       <?php include'myPhp.php' ?>
    

    并像这样使用它:

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

    【讨论】:

    • 这与之前删除的答案一样。除非调用 global $row; 这根本行不通。
    • 我自己试过了。对我来说,它是这样工作的。我使用了复数 $rows 变量。 (我之前删除的答案是一个问题,错误地了解更多细节,我将其更改为答案)
    【解决方案3】:

    解决了。 当我运行 Users.php 时,该表显示正确,但在通过执行 AddUsers.php 添加用户时显示该错误,其中包含 readfile('Users.php') 在它的末尾。 问题实际上出在readfile(Users.php)in 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){...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2019-07-10
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2018-02-24
      相关资源
      最近更新 更多