0

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 getError from browser console Please what have i done wrong. help!

3
  • Check in the Network section of your console what is the ajax request, its url and is it right (does it really reach selectMessages.php script)? Commented Nov 10, 2016 at 12:55
  • Please when is checked in the network section this is the error message I found which I don't really understand. Can you please Help? Commented Nov 11, 2016 at 14:00
  • Sorry for not showing the error message at first. This is what I meant. Please when is checked in the network section this is the error message I found which I don't really understand. Can you please Help? Notice: Undefined property: stdClass::$UserCookie in C:\xampp\htdocs\LibraryApp\selectMessages.php on line 16 Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\LibraryApp\selectMessages.php on line 20 Commented Nov 11, 2016 at 14:14

2 Answers 2

0

See this link for help. You have duplicates keys in your ng-repeat.

0

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);
?>
3
  • Hello Sir, i have tried your code and it doesn't give me any result and no error message in the network session of the browser. But when i remove the if statement in the php script you sent and i look into the netword session it gives me this error: "Notice: Undefined property: stdClass::$UserCookie in C:\xampp\htdocs\LibraryApp\selectMessages.php on line 11". Which I percieve that the php script is not getting the Variable UserCookie from angular. Can you suggest any other way to pass the variable from angular to the php script. Thanks Commented Nov 16, 2016 at 10:37
  • Thank You all... My problem is solved. I changed it and store the user details in a session after login. Below is my updated code. Commented Nov 17, 2016 at 12:38
  • <?php if(!isset($_SESSION)) {session_start(); $UserName = $_SESSION['UserName']; include_once 'connect.php'; $data = json_decode(file_get_contents("php://input")); $outputData = []; $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' => $UserName)); while($row = $statement->fetch()) {$outputData[] = $row;}print json_encode($outputData);}?> Commented Nov 17, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.