Search code examples
javascriptphpjsonbackendjsonencoder

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?


Solution

  • solved. the table was displaying correctly when i run Users.php but shows that error when adding a user by executing AddUsers.php which contains readfile('Users.php') at the end of it. the problem was actually in readfile(Users.php)in addUsers.php.

    readfile does not execute Users.php but only displays elements in it. therefore the JSON_encode never got executed and users weren't known to Users.php.

    i solved this problem by changing readfile('Users.php') in addUsers to include 'Users.php';

    Users.php code became :

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