希望您心情愉快:)。我正在尝试返回依赖于使用 AngularJS 和 php 的用户文本的数据。所以我创建了包含我的查询的文件 php,并在 AngularJS 中使用 $http.get。我的问题是我想在 mysql 查询中集成输入值,但总是使用 Angular 。我尝试了很多次但没有任何效果为了更清楚,这里有很多代码:
应用程序.js
app1.controller('autoCompleteCTRL',function($scope,$rootScope,$http) {
$scope.search = function(){
$scope.$watch('searchText', function() {
console.log($scope.searchText);
});
$http({
method: "get",
url: "../SearchResultsQuery.php",
dataType: 'json',
data: { 'userText': $scope.searchText},
async: false,
params: {action: "get"}
}).success(function(data, status, headers, config) {
alert(data);
}).error(function(data, status, headers, config) {
alert(error);
});
};
}
index.html
<input type="text" placeholder="Search for items" id="textFiled" class="input" ng-model="searchText" ng-change="search()" />
应用.php
<?php
$conn=pgsqlCon();
$searchText = mysql_real_escape_string(userText);
$query='SELECT * FROM planet_osm_roads AS a, to_tsquery_partial(\'%.$searchText.%\') AS query
WHERE ts_road @@ query';
$result = pg_query($conn,$query);
$myarray = array();
while ($row = pg_fetch_row($result)) {
$myarray[] = $row;
}
pg_close($conn);
echo json_encode($myarray);
?>
我希望你能理解我,谢谢你提前:)
最佳答案
试着像这样发出你的 http 请求:
app.controller('TestCtrl', function ($scope, $http){
$scope.search = function(){
$http.post('../app.php', {searchText: $scope.searchText})
.success(function (response){
console.log(response);
})
.error(function (response){
console.log(response);
});
}
});
应用.php
<?php
$data = file_get_contents("php://input");
$searchText = mysql_real_escape_string($data['searchText']);
/* database code */
?>
这应该有效。
关于php - 如何使用php和angularJS获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36773829/