-1

I am totally new in AngularJS and I am using PHP as the server script. I have a PHP class with connection() and getUsers() functions:

public function connect()
{
    $this->connection = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
    $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    return $this->connection;
}

public function getUsers()
{
    this->connect = this->connect(); 
    $sql = "SELECT * FROM login";
    $stmt = this->connect->prepare($sql);
    $stmt->execute();
    $res = $stmt->fetchAll();

    return json_encode($res);   
}

I am stuck at the angular part. How to call a function inside a url using $http.get() ?

    $http.get('/angular/conn.php')
      .success(function(result)
    {
        $scope.user = result;
    })
      .error(function(data, status)
    {
        $log.log(status);
    });

And another question on the side: when using angular with php their is no need to call the class and the function that we should use at the top of each html page ?

tereško
  • 56,151
  • 24
  • 92
  • 147
Lara Ch
  • 165
  • 2
  • 12

2 Answers2

2

You have to understand how the client/server architecture works for Angular. Angular is built to work along a web service, which is responsible for providing among other things, database access. The way you communicate with a web service is through API calls often made with AJAX. You can't simply call a php method from an Angular app like you would from another php class/script. You would have to design a web service in php in order to communicate with it.

Since you are trying to access a resoutce using a GET method on '/angular/conn.php', then your PHP web service should have a way to parse requests and respond accordingly considering the HTTP method, resource name, HTTP headers, etc. Since Angular is meant to work along REST web services, I would recommend you to use a PHP framework that fits such purpose. For example: Slim Framework

Here are some references you might find useful:

  1. What exactly is RESTful programming?

  2. REST Web Services

Community
  • 1
  • 1
Ricardo Leon
  • 125
  • 13
  • 1
    Your code does not work because your PHP backend does not have a structure to respond to REST calls such as the one you are making on your angular application. The solution would be to program a web service, which you may program in PHP – Ricardo Leon Aug 22 '16 at 05:10
-1

well the examples i saw where using then when you use GET method for example:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http({
        method : "GET",
        url : "your url here"
    }).then(function mySucces(response) {
        $scope.myWelcome = response.data;
    }, function myError(response) {
        $scope.myWelcome = response.statusText;
    });
});

try to put your complete url like: localhost:8080/mypage.php and print in console the result, you can first make the request in the browser to check if the server is responding the request

Pavul Zavala
  • 377
  • 2
  • 10