1

Beginner in AngularJS: I am passing parameters through http.get to the url (php file). On my php file I have mysql query in which I have to read those parameters in the where clause.

In brief: http://example.com/page.php?dept=some_dept&office=some_office is my page where I have my angular controller and view part. From this I want to get the url string/params and passed to angular-data.php from where I am fecthing the response. However, angular-data.php have a sql query in which I am planning to pass the dynamic values in WHERE clause i.e. the params from angular controller (dept and office) before getting response on my view page (url given above with params)

Below is my code so far:

page.php?dept=some_dept&office=some_office

var app = angular.module('myApp', []);
        app.controller('customersCtrl', function ($scope, $http) {
            $http({
                method: 'GET',
                url: 'angular-data.php',
                data: {
                    param1: type,
                    param2: office
                }
            }).then(function (response) {
                $scope.names = response.data.records;
            });
        });

page.php?dept=some_dept&office=some_office (continue...):

<tr ng-repeat="x in names">
    <td>
        {{$index + 1}}
    </td>
    <td>{{ x.Name}}.</td>
    <td>{{ x.Office}}</td>
</tr>

angular-data.php where I want params dynamically for WHERE clause:

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "local_server");
$result = $conn->query("SELECT * FROM `table` WHERE `Dept` = " . $_GET['dept'] . " AND `Office` = " . $_GET['office'] . "");
$outp = "";
while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {
        $outp .= ",";
    }
    $outp .= '{"Name":"' . $rs["Name"] . '",';
    $outp .= '"Office":"' . $rs["Office"] . '"}';
}
$outp = '{"records":[' . $outp . ']}';
$conn->close();
echo($outp);
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
moin
  • 89
  • 3
  • 11

3 Answers3

1
var app = angular.module('myApp', []);
        app.controller('customersCtrl', function ($scope, $http) {
            $http({
                method: 'GET',
                url: 'angular-data.php',
                params: {
            param1: 1,
            param2: 2
        }
            }).then(function (response) {
                $scope.names = response.data.records;
            });
        });

check more details

AngularJS passing data to $http.get request

Community
  • 1
  • 1
Yashveer Singh
  • 1,914
  • 2
  • 15
  • 23
1

I don't understand ,if you have a param type and office in html before send the http request. for example:

then:

data: {
    param1: $scope.type
}
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
betovaz81
  • 39
  • 6
1

In your SQL query,you are GETtingdept and office. You can pass these params as below.

you can use params : Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

$http({
          method: 'GET',
          url: 'angular-data.php',
          params: 'dept=some_dept, office=some_office'
    })

OR

$http({
          method: 'GET',
          url: 'angular-data.php',
          params: {
                     dept:"some_dept",
                     office:"some_office"
                  }
    })

More can be find here $http#config

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • Actually it's opposite, i am not getting parameters in SQL query. I want to pass the parameters to sql query. Not sure, if it's possible in that way. – moin Mar 01 '17 at 20:03
  • @Moin its difficult to get what you trying to say – RIYAJ KHAN Mar 02 '17 at 05:13
  • http://example.com/page.php?dept=some_dept&office=some_office is my page where I have my angular controller and view part. From this I want to get the url string/params and passed to angular-data.php from where I am fecthing the response. However, angular-data.php have a sql query in which I am planning to pass the dynamic values in WHERE clause i.e. the params from angular controller (dept and office) before getting response on my view page (url given above with params). Hope that make sense :) – moin Mar 02 '17 at 09:41