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)




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.


    $password = $request->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'”;


    // Mysql_num_row is counting table row


    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.

  • prashanth

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


    var request=$http({
    headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ }


    $scope.responsemessage=”successfulluy logedin”;
    $scope.responsemessage=”Username or Password is incorrect”;

    // php code is

    $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


    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){

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


    //scope.msgtxt=’Correct information’;
    else {

    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

    • Ankur Mehta

      It takes enter in echo i dont know how hence you are passing Enter and 1 which will not satisfy 1 condition