6 Replies - 744 Views - Last Post: 27 July 2009 - 10:37 PM

#1 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

PHP And AJAX Login With jQuery

Posted 23 July 2009 - 01:05 AM

Description: just change the connection path, tblusers, username and password fields as your own setting and enjoy it.this code will help you how to create your login page with AJAX using jQuery
<!--// index.php page -->
<link href="thems/login.css" rel="stylesheet" type="text/css" />
<link href="thems/global.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="includes/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#login_form").submit(function(){
		$("#spnmsg").removeClass().addClass('checking').text("Please wait checking... ").fadeIn(1000);

			$.post("login.php",{ un:$('#txtUsername').val(),pa:$('#txtPassword').val(),rand:Math.random() } ,function(response){
				if(response=="Valid")
				{
					$("#spnmsg").fadeTo(200,0.1,function(){
						$(this).html("Logging in...").addClass('logging').fadeTo(900,1,function(){
							document.location='admin_panel.php';
						});
					});
				}
				else if(response=="not Valid")
				{
					$("#spnmsg").fadeTo(200,0.1,function(){
						$(this).html("Invalid username or password").removeClass().addClass('error').fadeTo(900,1);
						$("#txtUsername").focus();
					});
				}		
			});		
 		return false; //not to post the  form physically
	});

	$("#btnLogin").click(function()
	{
		$("#login_form").trigger('submit');
	});
});
</script>
<style type="text/css">
</style>
<p> </p>
<p> </p>
<div id="div_login">
<form method="post" action="" id="login_form">
    <table width="300" border="0" align="center" cellpadding="2" cellspacing="0" class="4bdrs">
      <tr>
        <td width="95" class="texts">Username</td>
        <td width="197"><label>
          <input name="txtUsername" type="text" id="txtUsername" />
        </label></td>
      </tr>
      <tr>
        <td class="texts">Password</td>
        <td><label>
          <input name="txtPassword" type="password" id="txtPassword" />
        </label></td>
      </tr>
      <tr>
        <td align="right"><button id="btnLogin" type="button">Login</button></td>
        <td><span id="spnmsg" style="display:none"></span></td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>
<script type="text/javascript">
document.getElementById('txtUsername').focus();
document.getElementById('txtUsername').select();
</script>

<!-- // login.php -->
<?PHP
if(!isset($_SESSION)){session_start();}
include('includes/connection.php');
if(!isset($_POST['un']) && !isset($_POST['pa']))
{
	die("You can not use this page directly");
}
$un = $_POST['un'];
$pa = md5($_POST['pa']);
$strQuery = "SELECT * FROM tblUsers WHERE username='$un' AND password='$pa'";
$query_result = mysqli_query($con,$strQuery)or die(mysqli_error($con));
$query_num = mysqli_num_rows($query_result);
if($query_num>0)
{	
	$query_fetch = mysqli_fetch_assoc($query_result);
	$_SESSION['Username']=$query_fetch['username'];
	$_SESSION['Userid']=$query_fetch['id'];
	$group_str = "SELECT * FROM tblgroups WHERE `id`=".$query_fetch['groupid'];
	$group_query = mysqli_query($con,$group_str)or die(mysqli_error($con));
	$group_fetch = mysqli_fetch_assoc($group_query);
	$_SESSION['Group_Title']=$group_fetch['title'];
	
	if(isset($_GET['remember'])=="true")
	{
		setcookie('Username',$query_fetch['username'],time()+10);
	}
	echo "Valid";
}
else
{
	echo "not Valid";
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP And AJAX Login With jQuery

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: PHP And AJAX Login With jQuery

Posted 26 July 2009 - 05:54 PM

Looks nice! :) You should add mysql_real_escape_string around the $un variable though for security, can inject MySQL into that at the moment so it's unsecure.
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP And AJAX Login With jQuery

Posted 26 July 2009 - 10:54 PM

oops! i forgot it. thanks
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP And AJAX Login With jQuery

Posted 26 July 2009 - 11:03 PM

MageUK i didn't found any function for mysqli!
Was This Post Helpful? 0
  • +
  • -

#5 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP And AJAX Login With jQuery

Posted 27 July 2009 - 01:50 AM

k! changed it to your advice.
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: PHP And AJAX Login With jQuery

Posted 27 July 2009 - 01:50 AM

It's just the same according to php.net, string mysqli_real_escape_string ( mysqli $link , string $escapestr ). If you're using MySQLi why not use the OO interface? Very clean :-)
Was This Post Helpful? 0
  • +
  • -

#7 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: PHP And AJAX Login With jQuery

Posted 27 July 2009 - 10:37 PM

Quote: "It's just the same according to php.net, string mysqli_real_escape_string ( mysqli $link , string $escapestr )" i changed it. Quote: "If you're using MySQLi why not use the OO interface? Very clean :-)", Thanks i will use it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1