Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
63 views
in Technique[技术] by (71.8m points)

javascript - unable to get back end array data using JSON-encode

i have a script (Users.php) that uses JSON_encode to display an array of objects in an HTML table.

as in:

Users.php :

html //empty table

script //fills the table by using json encode to get php array and display the contents in the table

myPhp.php :

gets info from database and creates the array.

my php file is working just fine and so is my script. the only problem is when i use JSON_encode to get the array from php to the script it shows an error : Uncaught SyntaxError: Unexpected token '<' //on line 1 of php code

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

my 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();

?>

what I've tried:

I tried including the php file before using json_encode

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

this works when i run Users.php but if i add a user (by submitting the form in this webpage), add user file reads Users.php again after the user is added, users will end up not displaying and i will have the same error: Uncaught SyntaxError: Unexpected token '<' //in line 1 of myPhp.php

is there any other way to use JSON_encode that won't result in this error?

question from:https://stackoverflow.com/questions/65557370/unable-to-get-back-end-array-data-using-json-encode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not getting the same error that you are but MyUsers.php does not know about the variable you are trying to use:



    //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
        }
    }

There are a few different ways to get $rows in to myUsers.php.

One way that is easier to understand when you are learning is to do everything in one page: get your data at the top and render the HTML in the bottom.

MyUser2.php: I did not set up a database and hard coded the rows. Assuming that your database setup works you can removed the comments and the hard coded users. Note that since you are outputting a valid JSON array you do not have to re-parse it inside the javascript.

It will look like this in the browser:


    //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 file listing:

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

This will get you started. A good framework for learning PHP is CodeIgniter. Laravel is a great framework and more popular but it adds a lot of magical stuff that makes regular PHP harder to learn.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...