Creating A Registration And Login Using AngularJS - Part 1

In this tutorial we will create a registration form using AngularJS. The AngularJS is a structural framework for dynamic webpage nowadays. It is a kind of template that extends HTML ,and it use a directives to trigger the syntax that is in a structural form. This time we will try to collaborate angularjs with PHP script to make a registration form. By the way this tutorial is divided into two parts registration and login form, the one that we will try to work on is a registration form. Let's start coding... Creating A Form This is where the user will input their respected personal information. To do that open any kind of text editor that your computer have(notepad++, etc). Copy/paste the code below and name it "index.php"
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
  6. <script src = "js/angular.js"></script>
  7. <script src = "js/app.js"></script>
  8. </head>
  9. <body ng-app = "myModule" ng-controller = "myController">
  10. <nav class = "navbar navbar-default">
  11. <div class = "container-fluid">
  12. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodesters</a>
  13. </div>
  14. </nav>
  15. <div class = "row">
  16. <div class = "col-md-3"></div>
  17. <div class = "col-md-6 well">
  18. <h3 class = "text-primary">Creating A Registration And Login Using AngularJS - Part 1</h3>
  19. <hr style = "border-top:1px dotted #000;"/>
  20. <h4>Member Registration</h4>
  21. <br />
  22. <div ng-model="message" ng-show="showMessage" class="alert alert-info">{{message}}</div>
  23. <form ng-submit = "submitForm()">
  24. <div class = "form-group">
  25. <label>Username</label>
  26. <input type = "text" maxlength= "25" class = "form-control" ng-model = "members.username" ng-required = "true"/>
  27. </div>
  28. <div class ="form-group">
  29. <label>Password</label>
  30. <input type = "password" maxlength = "12" class = "form-control" ng-model = "members.password" ng-required = "true" />
  31. </div>
  32. <div class = "form-group">
  33. <label>Firstname</label>
  34. <input type = "text" maxlength = "30" class = "form-control" ng-model = "members.firstname" ng-required = "true"/>
  35. </div>
  36. <div class = "form-group">
  37. <label>Lastname</label>
  38. <input type = "text" maxlength = "30" class = "form-control" ng-model = "members.lastname" ng-required = "true"/>
  39. </div>
  40. <div class = "form-group">
  41. <button class = "btn btn-primary form-control"><span class = "glyphicon glyphicon-save"></span> Save Data</button>
  42. </div>
  43. </form>
  44. </div>
  45. </div>
  46. </body>
  47. </html>
The code above will try to submit the value of an input, and later submitted it via php script. The ng-required will prevent the submission of the data when the input field is null. The ng-submit will try to store all the data and sent it through a angularjs directives. Creating a Script for AngularJs Directives This is where the angularjs will be declared, and processed the request of the HTML form. To to make it worked, copy/paste the code below and name it "app.js"
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http, $timeout){
  3. $scope.members = {}
  4. $scope.submitForm = function(){
  5. var request = $http({
  6. method : "POST",
  7. url : "save_query.php",
  8. data : {
  9. 'username': $scope.members.username,
  10. 'password': $scope.members.password,
  11. 'firstname': $scope.members.firstname,
  12. 'lastname': $scope.members.lastname
  13. },
  14. headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
  15. })
  16. request.then(function(response){
  17. $scope.members.username = "";
  18. $scope.members.password = "";
  19. $scope.members.firstname = "";
  20. $scope.members.lastname = "";
  21. $scope.message = "Successfully Register A Member!";
  22. $scope.showMessage = true;
  23. $timeout(function(){
  24. $timeout(function(){
  25. $scope.showMessage = false;
  26. }, 3000);
  27. }, 2000);
  28. });
  29. };
  30. });
The code above will processed the following function, and will try to send the request to php script. Then it will store all the gathered data into the php web sever. Sending The Request To PHP Script This where the data will be stored and later on send to the web server. To do that, copy/paste the code below then name it "save_query.php"
  1. <?php
  2. $postdata = file_get_contents("php://input");
  3. $request = json_decode($postdata);
  4. $username = $request->username;
  5. $password = $request->password;
  6. $firstname = $request->firstname;
  7. $lastname = $request->lastname;
  8. $conn = new mysqli("localhost", "root", "", "db_member") or die(mysqli_error());
  9. $conn->query("INSERT INTO `member` (username, `password`, firstname, lastname) VALUES('$username', '$password', '$firstname', '$lastname')") or die(mysqli_error());
  10. ?>
The code above will receive the request from the angularjs directives. The file_get_contents will read all the stored data into a string type. The json_decode will accept the string data and convert it into a readable php script, then the php script will take care of it for storing the data in the web server. There you have it we created a registration form using angularjs. I hope that this tutorial help you to your projects. For the next tutorial we will try to do the login form based on the code that we used here. For more updates and tutorials, just kindly visit this site. Enjoy Coding!!

Tags

Add new comment