AngularJS POST request with PHP (Creating login form)

After searching through many articles for how to send  AngularJS POST data to PHP, the solutions i found were not up to the point. So, i decided to create a simple login form that will POST data to PHP & in the PHP file we can check the posted data against the values in our mysql database or whatever database, but i am checking the requested data against the static values & then the PHP script return the response back to the client side.

There are 3 files namely index.html, app.js, login.php

  • In the index.html, there is login form
  • In the app.js, Angular module & controller is defined
  • In the login.php, the validation is done (currently againt static values)

index.html

 app.js

 login.php

Copy the contents to their respective files & then run index.html.

Right Username/Password Combination: one / one

When you type this username/ password combination, you will get successfully logged in & when wrong combination, you will get Username or Password is incorrect as described in the app.js. Which is done with the response from the login.php.

If login.php return 1 then we get “Successfully Logged in” else we get “Username or Password is incorrect”.

 

  • Craig R Morton

    Nice post, thanks.

  • Guillermo Rodriguez

    Oriented to the need for developers, thank you very much! Excuse me for my bad english

  • Prateek Rathore

    Totally Working. Thanks Sir 🙂

  • Test

    nice but given all file like download option

  • Srinu Dola

    Please see the below code i am always getting “0” so Username or Password is incorrect, Please help me.

    email;

    $password = $request->password;

    die($email.’|’.$password);

    $servername = “localhost”;

    $username = “root”;

    $password = “root”;

    $dbname = “my_tbl”;

    $dbtbl = “admin_login”;

    // Create connection

    $conn = new mysqli($servername, $username, $password, $dbname, $dbtbl);

    // Check connection

    if ($conn->connect_error) {

    die(“Connection failed: ” . $conn->connect_error);

    }

    $sql = “SELECT * FROM $dbtbl WHERE a_username=’$email’ and a_password=’$password'”;

    $result=mysql_query($sql);

    // Mysql_num_row is counting table row

    $count=mysql_num_rows($result);

    if ($conn->query($sql) === TRUE) {

    echo “1”;

    } else {

    echo “0”;

    }

    ?>

    • I think, it is most likely due to the condition in the if,

      if ($conn->query($sql) === TRUE)

      You are check here if the query runs, which will run always. So it doesn’t matter if the username and password doesn’t match the one from the db. But query will always run, so always true.
      You need to check for something like if the query returned any row or not.
      Thanks.

  • prashanth

    hi i am always getting the username or pssword is incorrect, my code is like this

    $scope.login=function(){

    var request=$http({
    method:’POST’,
    url:’data/user.php’,
    data:{
    email:$scope.email,
    password:$scope.password
    },
    headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }
    });

    request.success(function(data){

    if(data==’1′)
    {
    $scope.responsemessage=”successfulluy logedin”;
    }
    else
    {
    $scope.responsemessage=”Username or Password is incorrect”;
    }
    });

    // php code is

    email;
    $password = $request->password;
    if($email == “one” && $password== “one”){
    echo “1”;
    }
    else {
    echo “0”;
    }
    ?>

    please help me

    • Your email varialbe in php is not defined, try changing it to,

      $email = $request->email;

      • prashanth

        thank you

  • prashanth

    hi,

    i am trying to display scope value from service in login page if its invalid useremail and password, but its not working
    my code is like this

    app.factory(‘loginService’, [‘$http’, ‘$location’, ‘sesseionservice’, function($http, $location, sesseionservice){
    return{

    // login function
    login:function(data, scope){
    var $promise=$http.post(‘data/user.php’,data); //send data to user.php
    $promise.then(function(msg){

    var uid=msg.data;

    if(uid){
    //scope.msgtxt=’Correct information’;
    sesseionservice.set(‘uid’,uid);
    $location.path(‘/home’);
    }
    else {

    $location.path(‘/login’);
    scope.msgtxt=’incorrect information’;

    }

    });
    },

    i want to display msgtxt value in login page, if username and password are not valid

  • Madhuri Kulkarni

    I get 405 error

    angular.js:8560 POST http://localhost:64502/login.php 405 (Method Not Allowed).. what could be the problem..?

  • Kruti patel

    i have one problem.we are creating one Ionic app.
    its good work on browser .but when i open the app in android device at that time not working.!
    So please help me.

  • chetan

    when i run your code, have got every time same error message ‘Username or Password is incorrect’. please give me solution