问题描述:

hello i am new in stackoverflow,

i trying to build a chat system with HTML , CSS ,PHP & angularjs

but i stack in the middle

my question is why my site doesnt live update with angularjs ?

i trying to get a live select Query from SQL server and when i try to change the value to update the row noting happen in my site only if i am refreshing the site its change the records

maybe i dont have enough knowlegh about angularjs

How i should fix this problem ?

this is my script: index.html

<html>

<style>

table, th, td {

border: 1px solid grey;

border-collapse: collapse;

padding: 5px;

}

table tr:nth-child(odd) {

background-color: #f1f1f1;

}

table tr:nth-child(even) {

background-color: #ffffff;

}

</style>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="customersCtrl">

<table>

<tr ng-repeat="x in names">

<td>{{ x.Name }}</td>

<td>{{ x.Message }}</td>

</tr>

</table>

</div>

<script>

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope, $http) {

$http.get("demo_index.php")

.then(function (response) {$scope.names = response.data.records;});

});

</script>

</body>

</html>

demo_index.php

 <?php

header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=UTF-8");

//Connection details [SQL]

$servername = "localhost";

$username = "****************";

$password = "***************";

$dbname="**************";

// Create connection

$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

$sql = "SELECT * From chat" ;

$outp = "";

$result = $conn->query($sql);

$num_rows = mysqli_num_rows($result);

if($num_rows>=0)

{

while($row = $result->fetch_assoc()){

if ($outp != "") {$outp .= ",";}

$outp .= '{"Name":"' . $row["name"] . '",';

$outp .= '"id":"' . $row["id"] . '",';

$outp .= '"Message":"'. $row["message"] . '"}';

}

$outp ='{"records":['.$outp.']}';

}

$conn->close();

echo($outp);

?>

相关阅读:
Top