1

I'm having a simple problem that I cannot seem to resolve. I'm trying to return a simple set of data from mysql database using angular/ajax and php. The problem is nothing populates in the HTML. No errors in debugger. Here is my code.

HTML:

<?php
include("includes/header.php");
?>
<body ng-app="testapp">
  <div ng-ctrl="HomeController">
    <input type="text" ng-model="name">
    <p>{{name}}</p>

    <ul ng-repeat="user in users">
      <li>{{user.fname user.lname}}</li>
    </ul>


  </div>
  <script type="text/javascript" src="angular/testapp.js"></script>
  <script type="text/javascript" src="angular/TestController.js"></script>
</body>
<?php
  include("includes/footer.php");
?>

ANGULAR/AJAX

    angular.module("testapp").controller("TestController", function($scope, $http){
      $scope.name = "";

      $http({
        method: "POST",
        url: "../includes/getUsers.php"
      }).success(function(data){
        $scope.users = data;
      });

    });

Finally PHP getUsers.php:

  include("connection.php");

  $query = "SELECT fname, lname FROM user"

  $rows = $db->query($query);
  $data = array();
  while ($row = $rows->fetchAll(PDO:FETCH_ASSOC)) {
    $data[] = $row;
  }


  echo json_encode($data);
5
  • any error you are getting in console? Commented Oct 15, 2016 at 3:27
  • I'm not seeing any errors in the console in developer tools using chrome. Commented Oct 15, 2016 at 3:28
  • Why use POST? Commented Oct 15, 2016 at 3:29
  • share your connection.php code Commented Oct 15, 2016 at 3:31
  • add an error handler and find out what it tells you. Can also inspect actual request in browser dev tools for clues Commented Oct 15, 2016 at 4:37

3 Answers 3

2

Try this ;)

This statement is causing problem:

while ($row = $rows->fetchAll(PDO:FETCH_ASSOC)) {

Here you are using constant FETCH_ASSOC of PDO class so replace : with ::

while ($row = $rows->fetchAll(PDO::FETCH_ASSOC)) {

and one more thing always set error_reporting(E_ALL); in development so that you can get all errors;

UPDATE

You should also add proper headers for JSON response also mentioned by @venkatesh-konatham:

header('Content-Type: application/json');
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

3 Comments

That was the problem. Thanks.
@EricSchumann If this answer solved you issue then accept it and close question;
Sorry. Newbie here.
0

Try first to call your php code to verify if it's returning something.

After that, try to add an .error() in the $http call to verify if really succeds the request

1 Comment

Verified that addUsers.php returns JSON string by including on index.php and echoing out to document.
0

I think problem is bind json data to angularjs. Can you try adding content type on your php code like below on the header

header('Content-Type: application/json');

Comments

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.