AngularJS Signup/Register using PHP/MySQLi

Getting Started

I've used CDN for Bootstrap, Angular JS and Angular Route so you need internet connection for them to work. This tutorial is a continuation of the previous tutorial, Angular JS Login with Session using PHP/MySQLi, which discuss on how to create a login page. We're gonna be using the files we created in the previous tutorial.

Creating our Database

First, we're going to create our database. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2. `memid` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(30) NOT NULL,
  4. `password` varchar(30) NOT NULL,
  5. `firstname` varchar(30) NOT NULL,
  6. `lastname` varchar(30) NOT NULL,
  7. `address` text NOT NULL,
  8. PRIMARY KEY(`memid`)
database sql

Updating our Main Page

Next, we update index.html by adding the ff. javascript files for our registration.
  1. <script src="js/controllers/signupCtrl.js"></script>
  2. <script src="js/services/signupService.js"></script>

Creating the link for Signup

Next, in the login.html, in the panel heading, add this to have a link to our registration form.
  1. <a class="pull-right btn btn-default btn-xs" href="#/signup" style="color:black">Signup</a>

Updating our Main Angular JS Script

Next, we update angular.js to include our signup form.
  1. var app = angular.module('app', ['ngRoute']);
  2.  
  3. app.config(function($routeProvider){
  4. $routeProvider
  5. .when('/', {
  6. templateUrl: 'login.html',
  7. controller: 'loginCtrl'
  8. })
  9. .when('/signup', {
  10. templateUrl: 'signup.html',
  11. controller: 'signupCtrl'
  12. })
  13. .when('/home', {
  14. templateUrl: 'home.html',
  15. controller: 'homeCtrl'
  16. })
  17. .otherwise({
  18. redirectTo: '/'
  19. });
  20. });
  21.  
  22. app.run(function($rootScope, $location, loginService){
  23. //prevent going to homepage if not loggedin
  24. var routePermit = ['/home'];
  25. $rootScope.$on('$routeChangeStart', function(){
  26. if(routePermit.indexOf($location.path()) !=-1){
  27. var connected = loginService.islogged();
  28. connected.then(function(response){
  29. if(!response.data){
  30. $location.path('/');
  31. }
  32. });
  33.  
  34. }
  35. });
  36. //prevent going back to login page if sessino is set
  37. var sessionStarted = ['/'];
  38. $rootScope.$on('$routeChangeStart', function(){
  39. if(sessionStarted.indexOf($location.path()) !=-1){
  40. var cantgoback = loginService.islogged();
  41. cantgoback.then(function(response){
  42. if(response.data){
  43. $location.path('/home');
  44. }
  45. });
  46. }
  47. });
  48. //prevent going back to singup page
  49. var signupcompleted = ['/signup'];
  50. $rootScope.$on('$routeChangeStart', function(){
  51. if(signupcompleted.indexOf($location.path()) !=-1){
  52. var signupsuccess = loginService.islogged();
  53. signupsuccess.then(function(response){
  54. if(response.data){
  55. $location.path('/home');
  56. }
  57. });
  58. }
  59. });
  60. });

Creating our Signup Form

Next, we create our sign up form and we're gonna name it as signup.html.
  1. <div class="col-md-4 col-md-offset-4">
  2. <div class="login-panel panel panel-primary">
  3. <div class="panel-heading">
  4. <h3 class="panel-title"><span class="glyphicon glyphicon-user"></span> Signup
  5. <a class="pull-right btn btn-default btn-xs" href="#/" style="color:black">Login</a>
  6. </h3>
  7. </div>
  8. <div class="panel-body">
  9. <form role="form" name="signupform">
  10. <div class="form-group">
  11. <input class="form-control" placeholder="Username" type="text" autofocus ng-model="user.username" required>
  12. </div>
  13. <div class="form-group">
  14. <input class="form-control" placeholder="Password" type="password" ng-model="user.password" required>
  15. </div>
  16. <div class="form-group">
  17. <input class="form-control" placeholder="Firstname" type="text" ng-model="user.firstname" required>
  18. </div>
  19. <div class="form-group">
  20. <input class="form-control" placeholder="Lastname" type="text" ng-model="user.lastname" required>
  21. </div>
  22. <div class="form-group">
  23. <input class="form-control" placeholder="Address" type="text" ng-model="user.address" required>
  24. </div>
  25. <button type="button" class="btn btn-lg btn-primary btn-block" ng-disabled="signupform.$invalid" ng-click="signup(user)"><span class="glyphicon glyphicon-check"></span> Verify</button>
  26. </form>
  27. </div>
  28. </div>
  29.  
  30. <div class="alert alert-danger text-center" ng-show="errorSignup">
  31. <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  32. {{ errorMsg }}
  33. </div>
  34. <div class="alert alert-success text-center" ng-show="successSignup">
  35. <button type="button" class="close" ng-click="clearMsg()"><span aria-hidden="true">&times;</span></button>
  36. {{ successMsg }}
  37. </div>
  38. </div>

Creating our Signup Controller

Next step is to create the controller for our sign up and we're gonna name it as signupCtrl.js.
  1. 'use strict';
  2.  
  3. app.controller('signupCtrl', function($scope, signupService){
  4. $scope.errorSignup = false;
  5. $scope.successSignup = false;
  6.  
  7. $scope.signup = function(user){
  8. signupService.signup(user, $scope);
  9. }
  10.  
  11. $scope.clearMsg = function(){
  12. $scope.errorSignup = false;
  13. $scope.successSignup = false;
  14. }
  15. });

Creating our Signup Service

Next, we create our sign up service and we're gonna name it as signupService.js
  1. 'use strict';
  2.  
  3. app.factory('signupService', function($http, $location, sessionService){
  4. return{
  5. signup: function(user, $scope){
  6. var validate = $http.post('signup.php', user);
  7. validate.then(function(response){
  8. //console.log(response);
  9. var uid = response.data.user;
  10. if(uid){
  11. sessionService.set('user',uid);
  12. $location.path('/home');
  13. }
  14.  
  15. else{
  16. $scope.successLogin = false;
  17. $scope.errorLogin = true;
  18. $scope.errorMsg = response.data.message;
  19. }
  20. });
  21. }
  22. }
  23. });

Creating our Signup API

Lastly, this is our Signup PHP code/api to insert user in our database.
  1. <?php
  2.  
  3.  
  4. $conn = new mysqli("localhost", "root", "", "angular");
  5.  
  6. if ($conn->connect_error) {
  7. die("Connection failed: " . $conn->connect_error);
  8. }
  9.  
  10. $out = array('error' => false);
  11.  
  12. $user = json_decode(file_get_contents('php://input'));
  13.  
  14. $username = $user->username;
  15. $password = $user->password;
  16. $firstname = $user->firstname;
  17. $lastname = $user->lastname;
  18. $address = $user->address;
  19.  
  20. $sql = "INSERT INTO members (username, password, firstname, lastname, address) VALUES ('$username', '$password', '$firstname', '$lastname', '$address')";
  21. $query = $conn->query($sql);
  22.  
  23. if($query){
  24. $nid = $conn->insert_id;
  25. $out['user'] = uniqid('ang_');
  26. $_SESSION['user'] = $nid;
  27. }
  28. else{
  29. $out['error'] = true;
  30. $out['message'] = 'Signup Failed';
  31. }
  32.  
  33. echo json_encode($out);
  34.  
  35. ?>
That ends this tutorial. Happy Coding :)

Add new comment