article

Friday, August 17, 2018

Ajax Login with jquery

Ajax Login with jquery mysql and bootstrap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax Login with jquery mysql and bootstrap</title>
<script>
$(document).ready(function(){
 $("#rolling").slideDown('slow');
});
$(document).ready(function(){
  $("#submit").click(function(){
     if($("#uname").val()=="" || $("#pass").val()=="") {
   $(".display").fadeTo('slow','0.99');
   $("msg").hide();
   $(".display").fadeIn('slow',function(){$(".display").html("<span id='error'>Please enter username and password</span>");});
   return false;
     }else{
    $(".display").html('<span class="normal"><img src="img/loader.gif"></span>');
    var uname = $("#uname").val();
    var pass = $("#pass").val();
     $.getJSON("server.php",{username:uname,password:pass},function(json)
     {
      // Parse JSON data if json.response.error = 1 then login successfull
      if(json.response.error == "1")
      {
       $(".display").css('background','#CBF8AF');
       $(".display").css('border-bottom','4px solid #109601');
       data = "<span id='msg'>Welcome "+uname+"</span>";
       window.location.href = "theme_profile.html";
       /*
     login successfull, write code to Show next page here
       */
      }
      // Login failed
      else
      {
       $(".display").css('background','#FFD9D9');
       $(".display").css('border-bottom','4px solid #FC2607');
       data = "<span id='error'>Error check username and password?</span>";
      }
       $(".display").fadeTo('slow','0.99');
       $(".display").fadeIn('slow',function(){$(".display").html("<span id='msg'>"+data+"</span>");});
     });
    return false;
   }
  });
   $("#uname").focus(function(){
    $(".display").fadeTo('slow','0.0',function(){$(".display").html('');});
   });
   $("#pass").focus(function(){
    $(".display").fadeTo('slow','0.0',function(){$(".display").html('');});
   });
});
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<!--Fontawesome CDN-->
</head>
<body>
<div class="container">
 <div class="d-flex justify-content-center h-100">
  <div class="card" id="rolling">
   <div class="card-header">
    <h3>Sign In</h3><span id="msg"></span>
    <div class="d-flex justify-content-end social_icon">
     <span><i class="fab fa-facebook-square"></i></span>
     <span><i class="fab fa-google-plus-square"></i></span>
     <span><i class="fab fa-twitter-square"></i></span>
    </div><p class="display"></p> <span id="msg"></span>
   </div>
   <div class="card-body">
   <form name="test" id="test" method="POST">
     <div class="input-group form-group">
      <div class="input-group-prepend">
       <span class="input-group-text"><i class="fas fa-user"></i></span>
      </div>
      <input type="text" name="uname" id="uname" class="form-control" placeholder="username">
     </div>
     <div class="input-group form-group">
      <div class="input-group-prepend">
       <span class="input-group-text"><i class="fas fa-key"></i></span>
      </div>
      <input type="password" name="pass" id="pass" class="form-control" placeholder="password">
     </div>
     <div class="row align-items-center remember">
      <input type="checkbox">Remember Me
     </div>
     <div class="form-group">
      <input type="submit" Value="Login" class="btn float-right login_btn" id="submit">
     </div>
    </form>
    </div>
     <div class="card-footer">
    <div class="d-flex justify-content-center links">
     Don't have an account?<a href="#">Sign Up</a>
    </div>
    <div class="d-flex justify-content-center">
     <a href="#">Forgot your password?</a>
    </div>
   </div>
   </div>
  </div>
 </div>
</div>
<style>
 
html,body{
background-size: cover;
background-repeat: no-repeat;
height: 100%;
font-family: 'Numans', sans-serif;
}
.container{
height: 100%;
align-content: center;
}
.card{
height: 370px;
margin-top: auto;
margin-bottom: auto;
width: 400px;
background-color: rgba(0,0,0,0.5) !important;
}
.social_icon span{
font-size: 60px;
margin-left: 10px;
color: #FFC312;
}
.social_icon span:hover{
color: white;
cursor: pointer;
}
.card-header h3{
color: white;
}
.social_icon{
position: absolute;
right: 20px;
top: -45px;
}
.input-group-prepend span{
width: 50px;
background-color: #FFC312;
color: black;
border:0 !important;
}
input:focus{
outline: 0 0 0 0  !important;
box-shadow: 0 0 0 0 !important;
}
.remember{
color: white;
}
.display {color:#fff;}
.remember input
{
width: 20px;
height: 20px;
margin-left: 15px;
margin-right: 5px;
}
.login_btn{
color: black;
background-color: #FFC312;
width: 100px;
}
.login_btn:hover{
color: black;
background-color: white;
}
.links{
color: white;
}
.links a{
margin-left: 4px;
}
</style>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//server.php
<?php
$host = "localhost";
$username_ = "root";
$passworddb = "";
$databasename = "testingdb";
$connect = mysql_connect($host, $username_, $passworddb) or die("Opps some thing went wrong");
mysql_select_db($databasename, $connect) or die("Opps some thing went wrong");
$username = $_GET['username'];
$pass = $_GET['password'];
$password = md5($pass);
$sql_check = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'") or die('Invalid query: ' . mysql_error());;
if(mysql_num_rows($sql_check))
{
 echo '{"response":{"error": "1"}}'; // login successfull
}
else
{
  echo '{"response":{"error": "0"}}'; //failed
}
?>

Related Post