jQuery AJAX login and registration Form using PHP MySQLi
Today, We want to share with you jQuery AJAX login and registration Form using PHP MySQLi.
In this post we will show you AJAX login and registration Form example, hear for Simple User Registration & Login Script in PHP and MySQLi we will give you demo and example for implement.
In this post, we will learn about Simple PHP Login System Using MySQL and jQuery AJAX with an example.
What is AJAX?
Fist of all AJAX Full form of Asynchronous JavaScript and XML.(interactive with databased).
AJAX can be mostly used for interactive communication(using data) with a Database.(like mysql,sql,oracle),
ajax is used to create more fast and interactive Application.It’s new way of data send very fast to client(Browser side to serverside data exchange).
create database and table in mysql using php
Database and Table
Create Database : localhost/phpmyadmin
create Database Sample : user_registration
Create Table : go to crate a table
Now click sql tab and excute this script
CREATE TABLE IF NOT EXISTS `table_users` ( `tuser_id` int(11) NOT NULL AUTO_INCREMENT, `tuser_name` varchar(27) NOT NULL, `tuser_email` varchar(68) NOT NULL, `tuser_password` varchar(250) NOT NULL, `tjoining_date` datetime NOT NULL, PRIMARY KEY (`tuser_id`) )
dbconfig.php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//All set aatribute } //Generate Error message display catch(PDOException $e){ echo $e->getMessage();//Show message } ?>
Simple user login form ajax php mysql
index.html
login_process.php
prepare("SELECT * FROM table_users WHERE tuser_email=:email"); $stmt->execute(array(":email"=>$user_email));//Execute query here $row = $stmt->fetch(PDO::FETCH_ASSOC);//fetch the data $count = $stmt->rowCount();//Total no of rows count //check both password are same or not if($row['user_password']==$password){ echo "ok"; // log in success so ok. $_SESSION['user_session'] = $row['user_id'];//set session } else{ //Information wrong. echo "email or password does not exist."; // Does not matched wrong details } } catch(PDOException $e){ echo $e->getMessage(); //if generated error show display error message } } ?>
Script.js
//Using jQuery check validatin $('document').ready(function() { /* start validation using jQuery */ $("#login-form").validate({ rules: { //For use password using id password: { required: true, }, //For use user_email using id user_email: { required: true, email: true }, }, //For use message using id messages: { password:{ required: "please enter your Good password(strong)" }, user_email: "please enter your Business email address", }, //Submit form submitHandler: submitForm }); /* End validation using jQuery */ /* Simple login Form submit */ function submitForm() { //All obejct cretaed using form id(login-form) var data = $("#login-form").serialize(); //request client(browser to server data send) $.ajax({ type : 'POST',//using POST - GET - PUT or DELETE method url : 'login_process.php', // data send url data : data, // all data varible send beforeSend: function() { $("#error").fadeOut(5000);//jquery function $("#btn-login").html(' sending The data ...'); }, //if data send server successfully retrive respnse success : function(response) { if(response=="ok"){ //if response success so $("#btn-login").html(' Signing In ... '); //loader images in process setTimeout(' window.location.href = "home.php"; ',6000); //redirect url in this page } else{ $("#error").fadeIn(1000, function(){ $("#error").html(''+response+' !'); //Error display for sign in $("#btn-login").html(' Sign In'); }); } } }); return false; } /* End AJAX using login FORM submit */ });
home.php
prepare("SELECT * FROM table_users WHERE tuser_id=:uid");//make a select query $stmt->execute(array(":uid"=>$_SESSION['user_session']));//query excute $row=$stmt->fetch(PDO::FETCH_ASSOC);//Fetch records ?>Login Form using jQuery Ajax and PHP MySQL //custom css callHello Simple Ajax to' Welcome to the New members page.