0

In my user controller, the lodgin function check if there's a data in inside my database and perform a condition whether it's true or false, My question is how can I pass the value of of $valid variable in my php and pass to it in my userlogin javascript? Sorry for my bad english.

PHP:

public function loginAction() {
  if (!isset($_POST['data']))
    jsonOut($response - > status = 'failed');

  $user_data = json_decode($_POST['data']);
  $response = new stdClass();

  $this - > users_model_instance - > student_username = $user_data - > username;
  $this - > users_model_instance - > student_password = $user_data - > password;

  $valid = $this - > users_model_instance - > Login();

  if ($valid) {

    jsonOut($response - > status = 'success');
  } else {

    jsonOut($response - > status = 'failed');
  }
}

JavaScript:

(function() {
  var app = angular.module("userlogin", []);

  app.controller("UserLoginController", ["$location", "ngDialog", "$scope", "httpRequest",
    function($location, ngDialog, $scope, httpRequest) {

      $scope.students = {};
      $scope.newStudents = {};
      $scope.startTest = false;
      $scope.students;

      $scope.submitLogin = function() {
        var request = {};
        request.url = '/account/users/login';
        request.method = 'POST';
        request.data = angular.toJson($scope.students);

        var onSuccess = function(data, status, headers, config, statusText) {

          if (status == 'true') {
            alert('success!');
            $location.url('/examdirection/');
          } else if (status == 'failed') {
            alert('failed!');
          }
        };
        httpRequest(request, $scope.response, onSuccess);
      };
    }
  ]);
})();
5
  • What the jsonOut() function do ? If you want to send back the $valid value you should send it as json (json_encode(array("valid" => $valid))) in your response and set the appropriate header (Content-type: application/json). The "data" part of your "onSuccess" function will contain this json (like : data.valid) Commented May 13, 2015 at 7:23
  • Anyway you should also take a look at the angular $http service to do async request. docs.angularjs.org/api/ng/service/$http Commented May 13, 2015 at 7:25
  • @Okazari I just used it to determine if my if else condition is working, can you edit my codes so I can figure it out what was your suggestions? i'm new to php and angular. Commented May 13, 2015 at 7:27
  • Are you using a framework in php or is it from scratch ? Commented May 13, 2015 at 7:34
  • It has a framework, but own framework of our previous developer. So i just took this project to finish but I'm having a hard time to find the solution and study the whole system. Commented May 13, 2015 at 7:41

1 Answer 1

1

Consider looking at $http to make your async requests. Here is an exemple :

Angular :

$scope.submitLogin = function() {
    //You don't need to serialize the data into json
    $http.post('/account/user/login', $scope.students).success(function(data){
        //We enter here when the http response status code is a success 2XX
        alert('success!');
        $location.url('/examdirection/');
    }).error(function(){
        //We enter here when the http response status code is an error 4XX, 5XX
        alert('failed!');
    });
};

Php (you'll need to find the way in your framework to set the response code of your $response):

if($valid){
    //It should look like $response->setStatusCode(200)
    //http_response_code is for pure php http manipulation
    http_response_code(200);
}else{
    //Choose the best http code in your case
    http_response_code(400);
}

Hope it'll help you out.

1
  • Thanks to your help, http_response_code(); work to my codes, all I need is to edit my codes in javascript to trigger the error, Thanks a lot! Commented May 13, 2015 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.