如何使用angular js和php显示来自mysql数据库的动态数据。当我更改datepicker中的值时,我想从datepicker发送带有值的sql查询。我尝试使用函数更新和$ http.get与params。发送查询后,我想在表格中显示数据。
我的html文件:
<body ng-app ="myApp">
<section ng-controller="myController">
<div class="container">
<div class="row">
<div class="form-group col-md-12">
<label class="text-left">Date:</label>
<input name="date" ng-model="user.date" ng-value="date" ng-change="update()"
type="text" class="form-control" placeholder="dd-mm-yyyy" id="date" required>
</div>
<table class="table table-striped">
<thead>
<tr>
<td>Date</td>
<td>Name and Surname</td>
<td>Number</td>
<td>E-mail</td>
<td>Phone</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in names">
<td>{{x.Date}}</td>
<td>{{x.Name}}</td>
<td>{{x.Number}}</td>
<td>{{x.Email}}</td>
<td>{{x.Phone}}</td>
</tr>
</tbody>
</table>
</div><!-- .row -->
</div><!-- .container -->
</section>
</body>
我的Js文件:
var app= angular.module('myApp', ['ngRoute']);
app.controller('myController', function($scope, $http) {
$scope.update = function() {
$http({
method: 'GET',
url: 'connect.php',
params: {date_event: user.date}
}).then(function (response) {$scope.names = response.data.records;});
}
});
我的php文件:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$host = "localhost";
$user = "root";
$pass = "password";
$database = "my_database";
$conn = mysql_connect($host, $user, $pass) or die ("Error Connection");
mysql_select_db($database) or die ("Database does not exist");
$date = $_GET['user.date'];
$sel = "SELECT * FROM users WHERE date_event='$date'";
$result = mysql_query($sel);
$outp = "";
while( $rs = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $rs["name"] . '",';
$outp .= '"Email":"' . $rs["email"] . '",';
$outp .= '"Number":"' . $rs["number"] . '",';
$outp .= '"Date":"' . $rs["date_event"] . '",';
$outp .= '"Phone":"'. $rs["phone"] . '"}';
}
$outp ='{"records":['.$outp.']}';
// $conn->close();
echo($outp);
?>