首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php和angylarJS获取数据

问如何使用php和angylarJS获取数据
EN

Stack Overflow用户
提问于 2016-04-21 23:16:15
回答 2查看 90关注 0票数 0

我希望你的心情很好:)。我试图返回的数据,这取决于文本从用户使用AngularJS和php。所以我创建了php文件,其中包含我的查询,并在AngularJS中使用$http.get。我的问题是我想在mysql查询中集成输入值,但总是使用angular。我尝试了很多次,但没有更清楚的工作,下面是muy代码:

app.js

代码语言:javascript
复制
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

代码语言:javascript
复制
<input type="text" placeholder="Search for items" id="textFiled" class="input"  ng-model="searchText" ng-change="search()" />

app.php

代码语言:javascript
复制
<?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);

?>

我希望你能理解我,谢谢你的帮助:)

EN

回答 2

Stack Overflow用户

发布于 2016-04-22 04:11:42

尝试像这样发出http请求:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?php
    $data = file_get_contents("php://input");
    $searchText = mysql_real_escape_string($data['searchText']);
    /* database code */
?>

这应该是可行的。

票数 2
EN

Stack Overflow用户

发布于 2016-04-22 13:45:16

在app.js文件中使用

  1. ,而不是以下行:

数据:{ 'userText':$scope.searchText}

写入:

参数文本:{ 'userText':$scope.searchText}

并删除此行:

获取参数:{

:"get"}

这样,它将通过请求URL将userText变量作为GET参数进行传输。

在app.php文件中使用

  1. ,而不是:

$searchText = mysql_real_escape_string(userText);

写入:

$searchText =mysql_real_escape_string($_GET‘’userText‘);

这将用来自客户端应用程序(AngularJS)的文本填充您的$searchText php变量。我不确定你的postgresql查询。您的问题标题是postgres mysql-如何获取数据...,但在您的php代码示例中,您使用postgres扩展来构建查询。但我想那是另一回事了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36773829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档