I want to display data which I select from a mySQL database using PHP. The data should be shown in angular variables. I can see the JSON when clicking a button I installed. But the data will not show up in the angular variables. So I think the problem is the communication between php and angular on the way back from php to angular.
Here are the files I use. Where do i have to make changes to get what I want?
Thank you!
index.html
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<title>nutrimeal</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="container-fluid">
<div class="col-sm-10 content">
<h1>M E A L S</h1>
<form action="./php/meal_select.php" method="post">
<input class="btn btn-default" type="submit" value="display meals"/>
</form>
<div ng-controller = "MealsController as meals">
<p>The meals should be shown underneath</p>
<div ng-repeat="meal in meals">
{{ meal.name }}
{{ meal.daytime }}
</div>
</div>
</div>
</div>
<!-- Javascript references -->
<script src="./js/vendors/bootstrap.js"></script>
<script src="./js/vendors/angular.js"></script>
<script src="./js/app.js"></script>
</body>
</html>
app.js
var app = angular.module('MyApp', ['ngRoute'])
// MealsController
app.controller('MealsController', [ '$http', function($scope, $http) {
$scope.displayData = function(){
var meals = $http.get('./backend/meal_select.php')
// .then(function (response) {$scope.accounts = response.data.records;} );
.then(function (data) {
$scope.meals = data });
};
$scope.displayData()
}]);
meal_select.php
<?php
// connection settings
$servername = "localhost";
$username = "root";
$password = "qwertz";
$dbname = "example_nutrition";
// create connection to db
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// set SQL command
$sql = "SELECT meal_id, name, daytime FROM meal";
// query db
$result = $conn->query($sql);
// show results
if ($result->num_rows > 0) {
// output data of each row
$r=array()
while($row = $result->fetch_assoc()) {
$r=[] = $row;
//simple output
// echo " Account Name: " . $row["account_name"].
// " Balance: " . $row["initial_balance"].
// " Currency: " . $row["currency"]. "<br>";
// output as JSON
//$json_array = json_encode($row);
$json_array = json_encode($r);
header('Content-type: application/json');
echo $json_array;
}
}
// if nothing was found
else {
echo "0 results";
}
// close connection to db
$conn->close();
?>
JSON displayed after clicking the button
{"meal_id":"1","name":"cheeseburger","daytime":"lunch"} {"meal_id":"2","name":"chili con carne","daytime":"lunch"}
var