Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>AngularJS Post data with PHP</title>
- <link rel="stylesheet" href="// maxcdn.bootstrapcdn.co m/bootstrap/3.2.0 /css/bootstrap.min.css">
- <script src=" https://ajax.googleapis.co m/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>
- app.js
- 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";
- }
- });
- }
- });
- login.php
- <?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";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement