Skip to content

Instantly share code, notes, and snippets.

@mbaljeetsingh
Created September 10, 2014 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaljeetsingh/664556065d4817460c72 to your computer and use it in GitHub Desktop.
Save mbaljeetsingh/664556065d4817460c72 to your computer and use it in GitHub Desktop.
Angular Login POST request
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";
}
});
}
});
<!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>
<?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";
}
?>
@mbaljeetsingh
Copy link
Author

AngularJS POST request with PHP (Creating login form)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment