CHARAFI Saad
CHARAFI Saad

Reputation: 1440

Retrieve an object (PHP&ANGULARJS)

I want to retrieve an object sent from an Angular function to a PHP program. Here is my view:

<ion-view>
<ion-content padding="true">
<div class="card">
  <div class="item item-divider text-center">
    Authentification
  </div>
  <div class="item item-text-wrap text-center">
    <form>
      <div class="list">
        <label class="item item-input">
          <input type="text" placeholder="Nom d'utilisateur" ng-model="user.username">
        </label>

        <label class="item item-input">
          <input type="password" placeholder="mot de passe" ng-model="user.password">
        </label>


      </div>
    </form>

  </div>
  <div class="item item-divider text-center">
    <a  href="" class="button button-positive button-small" ng-click="logIn(user)">
      <i class="ionicons ion-android-share"></i>
      Identifiez moi, vite !
    </a>
    <a  href="" class="button button-energized button-small">
      <i class="ionicons ion-android-mail"></i>
      Mot de passe perdu !
    </a>
  </div>
</div>
<button class="button button-large button-full button-positive">
  je n'ai pas de compte
</button>

Here is controller :

'use strict';

app
.controller('homepageIndex',function ($scope) {
})
.controller('homepageLogin',function ($scope , userProvider) {
  $scope.user={};

  $scope.logIn = function (user) {
    console.log($scope.logIn);
    console.log($scope.user);
    userProvider.logIn(user);
  }


 })
 ;

Here is my userProvider.js

'use strict';

app.factory('userProvider', function ($rootScope , $http) {

  function logIn(user) {
    var url='http://127.0.0.1:100/suitecrm/service/rest.php';

    $http.post(url,user)
      .success(function (response) {
      console.log(response);
        console.log(url);

      });

  }

  return {
    logIn: logIn
  }
});

In my file rest.php I want to retrieve this object user which contains username and password:

$username =$_POST['username'];
$password =$_POST['password'];

This method doesn't work i want to know how to retrieve username and password in my rest.php Thank u for ur help.

Upvotes: 4

Views: 76

Answers (2)

CHARAFI Saad
CHARAFI Saad

Reputation: 1440

@Mohit Tanwani

$request = json_decode(file_get_contents('php://input'));

You can put this at the start of your PHP script. $request will then be an stdClass object with the data as properties.

$username = $request->username;
$password = $request->password;

Alternatively, if you prefer to work with it as assoc array, use:

$request = json_decode(file_get_contents('php://input'), TRUE);

and access data like:

$username = $request['username'];
$password = $request['password'];

Upvotes: 1

Mohit Tanwani
Mohit Tanwani

Reputation: 6628

Try following code.

var req = {
 method: 'POST',
 url: 'http://127.0.0.1:100/suitecrm/service/rest.php',
 data: { username: 'username', password:  'password' }
}

$http(req).then(function(){
   //Success
}, function(){

});

Upvotes: 4

Related Questions