AnuglarJs

User login using Angular JS in PHP and MySQL

We are providing an example of Angular Login application. To develop this app, we have used HTML, CSS, Mysql, PHP and AngularJS.

In this example, we have a simple login page. Whenever user provides username and password, they will be authenticated and redirected to a home page on success. If authentication fails, the user is notified with an error message.

We have the main base page i.e. index.php where all other views are loaded. Following code explains the index page. Base script files which are required to run an application, are loaded when the application is bootstrapped.

Please look into the AngularLoginController code as shown below. Now, we need a javascript to hold all the configuration details required for an application to run. In this file, we configure the routes for an application which is done using UI Router.

<script>
    angular.module('AngularJSLogin', [])
    .controller('AngularLoginController', function($scope, $http){
        this.loginForm = function(){
            var user_data='user_email='+this.inputData.email+'&user_password='+this.inputData.password;
            $http({
                method : "POST",
                url : "login.php",
                data: user_data,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function mySuccess(response) {
               console.log(response.data);
                if ( response.data === 'correct') {
                    window.location.href = 'welcome_dashboard.php';
                } else {
                    $scope.errorMsg = "Invalid Email and Password";
                }
            }, function myError(response) {
                console.log('Error Handle');
            });                    
        }
    });
</script>

As you seen in the above we called a login.php  using router where we are doing server part

<?php
 
//include database connection file
require_once 'db_config.php';
 
//echo '<pre>'; print_r($_POST);
// verifying user from database using PDO
$stmt = $DBcon->prepare("SELECT user_email, user_password from user WHERE user_email='".$_POST['user_email']."' && user_password='".$_POST['user_password']."'");
$stmt->execute();
$row = $stmt->rowCount();
if ($row > 0){
    echo "correct";
} else{
    echo 'wrong';
}
 
?>

Full source code available at here GitHUB

Related Articles

  1. How to install Gulp 4
  2. Why We Need WordPress Website Backup and Restore
  3. Why all sites now require SSL (https)
Developer Diary

Share
Published by
Developer Diary

Recent Posts

The Power of Email Marketing: Boosting Your Business’s Success

Introduction Even with the abundance of digital communication channels available today, email marketing is still…

23 hours ago

Laravel vs Node Js: Which One Is Good For What?

Introduction In the world of web development, selecting the correct framework is critical. Laravel and…

3 months ago

Docker Cheatsheet: Essential Commands and Explanations

Introduction By enabling containerization, Docker has transformed the way software is built, deployed, and managed.…

8 months ago

Difference between Memcached and REDIS – Secrets of Caching

Introduction Speed and efficiency are critical in the ever-changing world of web development. Effective caching…

8 months ago

How to Revert a Git Commit: A Simple Example

Introduction Git, a popular version control system in software development, enables developers to track changes,…

8 months ago

What is Git Stash and Why Do You Need It

Introduction Are you tired of grappling with tangled changes and unfinished work while using Git?…

8 months ago