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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…