我希望你的心情很好:)。我试图返回的数据,这取决于文本从用户使用AngularJS和php。所以我创建了php文件,其中包含我的查询,并在AngularJS中使用$http.get。我的问题是我想在mysql查询中集成输入值,但总是使用angular。我尝试了很多次,但没有更清楚的工作,下面是muy代码:
app.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()" />app.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);
?>我希望你能理解我,谢谢你的帮助:)
发布于 2016-04-22 04:11:42
尝试像这样发出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);
});
}
});app.php
<?php
$data = file_get_contents("php://input");
$searchText = mysql_real_escape_string($data['searchText']);
/* database code */
?>这应该是可行的。
发布于 2016-04-22 13:45:16
在app.js文件中使用
数据:{ 'userText':$scope.searchText}
写入:
参数文本:{ 'userText':$scope.searchText}
并删除此行:
获取参数:{
:"get"}
这样,它将通过请求URL将userText变量作为GET参数进行传输。
在app.php文件中使用
$searchText = mysql_real_escape_string(userText);
写入:
$searchText =mysql_real_escape_string($_GET‘’userText‘);
这将用来自客户端应用程序(AngularJS)的文本填充您的$searchText php变量。我不确定你的postgresql查询。您的问题标题是postgres mysql-如何获取数据...,但在您的php代码示例中,您使用postgres扩展来构建查询。但我想那是另一回事了。
https://stackoverflow.com/questions/36773829
复制相似问题