I want to load data from mysql database using angularjs.
This is how the application works; Users login and their username is stored in a cookie. This username is displayed on the home page
I want to get this value and pass to php script through angularjs to fetch data from a table based on the username with a where clause
This is my php scrip
<?php
include_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));
$statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc');
$statement->execute(array(
'UserName' => $data->UserCookie
));
while($row = $statement->fetch()) {
$data[] = $row;
}
print json_encode($data);
?>
This is my angular script
var app = angular.module('Library', ["ngCookies"]);
app.controller('LibraryController', function($scope, $http, $interval, $cookies) {
//get cookie user name
$scope.UserCookie = $cookies.get('cookieUserName');
//set cookie user name
$scope.setCookie = function(val){
$cookies.put('cookieUserName', val);
}
//display messages
$scope.loadMessages = function() {
$http.post('selectMessages.php',{'UserName': $scope.UserCookie})
.success(function(data) {
$scope.Messages = data;
})
}
//display messages at an interval of 1 seconds
$interval($scope.loadMessages, 1000);
});
This is my home page
<!DOCTYPE html ng-app="Library" ng-controller="LibraryController">
<html>
<head>
<title></title>
</head>
<body ng-init="loadMessages()">
<div class="form-group">
<label for="Subject">User</label>
<input type="text" ng-model="UserCookie" name="UserCookie" class="form-control" id="UserCookie" required="required"></input>
</div>
<input type="search" class="form-control" ng-model="searchMessage" placeholder="Search"> <br />
<table class="table table-hover">
<thead>
<tr>
<th>Message (Sender)</th>
</tr>
</thead>
<tbody>
<tr ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" style="font-size: 16px" ng-repeat="message in Messages | filter:searchMessage | limitTo:20">
<td>{{ message.Subject }} (<span style="color:green"> {{ message.FromUser }})</span></td>
<td>
<button class="btn btn-success" ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" > view </button>
</td>
</tr>
</tbody>
</table>
</body>
</html>
this is the error i get Error from browser console Please what have i done wrong. help!
See this link for help. You have duplicates keys in your ng-repeat.
According to your updated answer and the error you are receiving there are some hidden errors/problems in your code. You are getting the input, decode it from json and hold it in $data
variable (and json_decode
returns object as long as you don't pass true as second parameter). And in the while
loop you are trying to add new element to $data
as it is array
, but it is not.
And here is how it should look like:
<?php
include_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));
$outputData = [];
if($data && isset($data->UserCookie)) {
$statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc');
$statement->execute(array(
'UserName' => $data->UserCookie
));
while($row = $statement->fetch()) {
$outputData[] = $row;
}
}
print json_encode($outputData);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.