Created
September 10, 2014 07:10
-
-
Save mbaljeetsingh/664556065d4817460c72 to your computer and use it in GitHub Desktop.
Angular Login POST request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('angularPostPHP', []); | |
app.controller('loginCtrl', function ($scope, $http) { | |
$scope.login = function () { | |
var request = $http({ | |
method: "post", | |
url: "login.php", | |
data: { | |
email: $scope.email, | |
password: $scope.password | |
}, | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' } | |
}); | |
/* Successful HTTP post request or not */ | |
request.success(function (data) { | |
if(data == "1"){ | |
$scope.responseMessage = "Successfully Logged In"; | |
} | |
else { | |
$scope.responseMessage = "Username or Password is incorrect"; | |
} | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>AngularJS Post data with PHP</title> | |
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script> | |
<script src="app.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>AngularJS ajax POST with PHP</h1> | |
<div ng-app='angularPostPHP' ng-controller='loginCtrl'> | |
<input class="form-control" type="text" ng-model="email" placeholder="Enter Your Email"><br> | |
<input class="form-control" type="password" ng-model="password" placeholder="Enter Your Password"><br> | |
<button class="btn btn-success" ng-click="login()">Login</button><br> | |
<span>{{responseMessage}}</span> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check username or password from database | |
$postdata = file_get_contents("php://input"); | |
$request = json_decode($postdata); | |
$email = $request->email; | |
$password = $request->password; | |
if($email == "one" && $password== "one"){ | |
echo "1"; | |
} | |
else { | |
echo "0"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AngularJS POST request with PHP (Creating login form)