Advertisement
Guest User

ang

a guest
Aug 3rd, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>AngularJS Post data with PHP</title>
  6. <link rel="stylesheet" href="// maxcdn.bootstrapcdn.co m/bootstrap/3.2.0 /css/bootstrap.min.css">
  7. <script src=" https://ajax.googleapis.co m/ajax/libs/angularjs /1.2.24/angular.min.js"></script>
  8. <script src="app.js" type="text/javascript"></script>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1>AngularJS ajax POST with PHP</h1>
  13. <div ng-app='angularPostPHP' ng-controller='loginCtrl'>
  14. <input class="form-control" type="text" ng-model="email" placeholder="Enter Your Email"><br>
  15. <input class="form-control" type="password" ng-model="password" placeholder="Enter Your Password"><br>
  16. <button class="btn btn-success" ng-click="login()">Login</button><br>
  17. <span>{{responseMessage}}</span>
  18. </div>
  19. </div>
  20. </body>
  21. </html>
  22. app.js
  23. var app = angular.module('angularPostPHP', []);
  24. app.controller('loginCtrl', function ($scope, $http) {
  25. $scope.login = function () {
  26. var request = $http({
  27. method: "post",
  28. url: "login.php",
  29. data: {
  30. email: $scope.email,
  31. password: $scope.password
  32. },
  33. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  34. });
  35. /* Successful HTTP post request or not */
  36. request.success(function (data) {
  37. if(data == "1"){
  38. $scope.responseMessage = "Successfully Logged In";
  39. }
  40. else {
  41. $scope.responseMessage = "Username or Password is incorrect";
  42. }
  43. });
  44. }
  45. });
  46. login.php
  47. <?php
  48. // check username or password from database
  49. $postdata = file_get_contents("php://input");
  50. $request = json_decode($postdata);
  51. $email = $request->email;
  52. $password = $request->password;
  53. if($email == "one" && $password== "one"){
  54. echo "1";
  55. }
  56. else {
  57. echo "0";
  58. }
  59.  
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement