Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am using angular js script to fetch data from external php file encoded in json in html page. I have used $http.get(page2.php) method to fetch json encoded array written in another file. But the problem is its not showing any output just a blank screen I dont know where am I doing wrong.

Here's page1.html

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var app = angular.module('myapp',[]);
app.controller('myctrl',function($scope,$http){
$http.get('page2.php').success(function(response){
$scope.names = response;
});
});
</script>
</head>
<body>   
<div>
<table ng-app="myapp" ng-controler="myctrl" >
<tr ng-repeat="x in names">
<td>{{ x.id }}</td>
<td>{{ x.name }}</td>
<td>{{ x.age }}</td>
</tr>
</table>
</div>
</body>
</html>

Here's page2.php

<?php

$con = mysqli_connect('localhost','root','','practice');
if(!$con){
die("couldnt connect".mysqli_error);
}
$query = "SELECT * FROM records";
$result = $con->query($query);
$r = array();
if( $result->num_rows>0){
while($row = $result->fetch_assoc()){
$r[] = $row;
}
}
$res = htmlspecialchars(json_encode($r));
echo $res;
?>

I can't figure out where am I doing wrong.

share|improve this question

closed as off-topic by jonrsharpe, HaveNoDisplayName, John Conde, Jay Blanchard, Nisse Engström Jul 7 '15 at 13:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jonrsharpe, HaveNoDisplayName, John Conde, Jay Blanchard, Nisse Engström
If this question can be reworded to fit the rules in the help center, please edit the question.

    
You're error checking your connection, but not the query. – Jay Blanchard Jul 7 '15 at 12:44
    
Is there any error on browser console ? Check your connection to database .Check by adding console.log(response); in success callback – Rahul Naik Jul 7 '15 at 12:45
    
NO error just a blank screen – Share fun Jul 8 '15 at 7:16
    
query is fine working good but it display nothing.. @JayBlanchard Blanchard – Share fun Jul 9 '15 at 5:14

If you're sure you're getting data from your query, you just need to send it as JSON. Add this before the echo $res; line:

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

share|improve this answer
    
it is showing the converted data when i run php file alone but as i call from html page it shows nothing. @kiswa – Share fun Jul 8 '15 at 7:13
    
Try using something like Postman and see what that shows when it calls the PHP file. – kiswa Jul 8 '15 at 12:06
    
whats that for ?? @kiswa – Share fun Jul 9 '15 at 5:12
    
It lets you test an API directly. – kiswa Jul 9 '15 at 11:25
    
I am not using the api key...how would that resolve my issue.?? – Share fun Jul 9 '15 at 12:04

Here is the working code, It may help you:

PHP:

<?php
$con = mysqli_connect('localhost','root','','practice');
if(!$con){
die("couldnt connect".mysqli_error);
}
$query = "SELECT * FROM records";
$result = $con->query($query);
$r = array();
if( $result->num_rows>0){
while($row = $result->fetch_assoc()){
$r[] = $row;
}
}
$res = json_encode($r);
echo $res;
?>

HTML

<html>
    <head>
    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script>
    $(function(){
    var app = angular.module('myapp',[]);
    app.controller('myctrl',function($scope,$http){
    $http({method:'GET', url:'page2.php'}).success(function(response){
    $scope.names = response;
    });
    });
    });
    </script>
    </head>
    <body>   
    <div>
    <table  ng-app="myapp" ng-controller="myctrl" >
    <tr ng-repeat="x in names">
    <td> {{ x.id }} </td>
    <td>{{ x.name }}</td>
    <td>{{ x.age }}</td>
    </tr>
    </table>
    </div>
    </body>
    </html>
share|improve this answer
    
no not working . I've added an alert on php page and its not calling . it means its not going to that page @Kiran LM – Share fun Jul 8 '15 at 7:16
    
note that your code not spelled correctly ng-controler="myctrl" instead of ng-controller="myctrl", may be that's the problem. The above code works on my system. – Kiran LM Jul 8 '15 at 7:25
    
If you're using google chrome, check the network tab by inspecting element. It will show you the response from php – Kiran LM Jul 8 '15 at 7:29
    
no change i've added three alerts in your script its not even reading this @Kiran LM – Share fun Jul 8 '15 at 7:41
    
its showing the alerts now after removing this function "$(function(){ " in your script . But still cant fetch response data i am not getting where am i doing wrong please check it will be of great help. – Share fun Jul 8 '15 at 8:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.