如何使用php和angylarJS获取数据

如何使用php和angylarJS获取数据,php,mysql,angularjs,Php,Mysql,Angularjs,我希望你心情好:)。我试图使用AngularJS和php从用户返回依赖于文本的数据。因此,我创建了包含我的查询的php文件,并在AngularJS中使用$http.get。我的问题是我想在mysql查询中集成输入值,但总是使用angular。我尝试了很多次,但没有什么比这更清楚了,下面是muy代码: app.js app1.controller('autoCompleteCTRL',function($scope,$rootScope,$http) { $scope.search = f

我希望你心情好:)。我试图使用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);

?>
<?php
    $data = file_get_contents("php://input");
    $searchText = mysql_real_escape_string($data['searchText']);
    /* database code */
?>


我希望您能理解我的意思,谢谢您的帮助:)

请尝试这样发出您的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

$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);

?>
<?php
    $data = file_get_contents("php://input");
    $searchText = mysql_real_escape_string($data['searchText']);
    /* database code */
?>


这应该是可行的。

尝试以如下方式发出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

$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);

?>
<?php
    $data = file_get_contents("php://input");
    $searchText = mysql_real_escape_string($data['searchText']);
    /* database code */
?>

这应该行得通

  • 在你的app.js文件中,而不是这一行:
  • 数据:{'userText':$scope.searchText}

    写:

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

    并删除此行:

    参数:{action:“get”}

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

  • app.php文件中,而不是:
  • $searchText=mysql\u real\u escape\u字符串(userText)

    写:

    $searchText=mysql\u real\u escape\u字符串($\u GET['userText'])

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

  • 在你的app.js文件中,而不是这一行:
  • 数据:{'userText':$scope.searchText}

    写:

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

    并删除此行:

    参数:{action:“get”}

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

  • app.php文件中,而不是:
  • $searchText=mysql\u real\u escape\u字符串(userText)

    写:

    $searchText=mysql\u real\u escape\u字符串($\u GET['userText'])



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

    先看看这个谢谢你的帮助。我已经读过这篇文章,但我不明白file\u get\u内容的用法(“php://input"). 我在php文件中编写查询,所以我需要使用这一行吗?PS:我没有使用json数据,我只是想用angularJS读取输入,并将此变量集成到查询中。首先,感谢您的帮助。我已经读过这篇文章,但我不明白file\u get\u内容的用法(“php://input"). 我在php文件中编写查询,所以我需要使用这一行吗?PS:我没有使用json数据,我只是想用angularJS读取输入并将此变量集成到查询中。我得到了相同的错误“POST 500(内部服务器错误)”。我想是因为我写了$data=file\u get\u contents(“php://input"). 使用这条线的目的是什么?你能帮我吗?。我试过手动时间,但没有成功:(php://input 是一个允许您从请求体读取原始数据的流。您希望使用$_GET检索数据,然后检查@nostop solution。对于500错误,您的php代码中一定有错误。您是对的@Ayoub Oulaika,我在php文件中遇到了问题。当我尝试放置示例参数时:{'userText':'rabat'}在要替换为tou-tsquery-partial(\'.%.$searchText.%\')的查询中,tou-tsquery-partial(\'%rabat%\')起作用,所以问题是查询中变量的语法。我尝试了许多表达式,但都不起作用:(当我尝试添加:if(isset($\u GET['userText']){$user=$\u GET['userText'];}和在查询中:tou-tsquery partial(\'%$user%\'),没有错误,但没有显示。我收到了相同的错误“POST 500(内部服务器错误)”。我想是因为我写了$data=file\u get\u contents(“php://input)。使用这条线的目的是什么?你能帮我吗?我试过手动时间,但没用:(php://input 是一个允许您从请求体读取原始数据的流。您希望使用$_GET检索数据,然后检查@nostop solution。对于500错误,您的php代码中一定有错误。您是对的@Ayoub Oulaika,我在php文件中遇到了问题。当我尝试放置示例参数时:{'userText':'rabat'}在要替换为tou-tsquery-partial(\'.%.$searchText.%\')的查询中,tou-tsquery-partial(\'%rabat%\')起作用,所以问题是查询中变量的语法。我尝试了许多表达式,但都不起作用:(当我尝试添加:if(isset($\u GET['userText']){$user=$\u GET['userText'];}和在查询中:tou-tsquery partial(\'%$user%\'),没有错误,但没有显示。嗨,谢谢你的帮助,我现在就试试。啊,是的,我使用postgres数据库。我遇到了相同的错误。我想问题出在这行:$searchText=mysql\u real\u escape\u string($\u GET['userText'))。你认为呢?为了确定,我需要看看你会出现什么错误。可能有很多原因。现在我看到在angularjs应用程序url中,你请求的脚本名称与app.php不同。这可能是原因吗?另一件事,如果你想检查服务器脚本是否工作正常,只需键入你的浏览器地址栏:。This将直接执行脚本。如果您没有看到错误,也许您可以在php脚本的顶部添加错误报告(E_ALL);行。我只是在post中编写'app.php',但我使用的是正确的文件。我做了一个更改,而不是$searchText=mysql_escape_字符串($\u GET['userText'])我编写$searchText=pg escape_字符串($\u GET['userText']),没有错误,但我没有得到任何数据。我仍然被困在这个问题上:'(嗨,谢谢