Jump to content

Not sure why login form isn't working w/ JQuery


webguync

Recommended Posts

Hi,

 

I wanted to try out a PHP/MySQL/Jquery login form that I found code for online and integrate it for my needs, but can't get it to work. Not exactly sure where the script is failing for me ither. Any hints on debugging?

 

Code I am using below:

 

 

FORM

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ajax Login in php using jquery's fading effects</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".loading").hide();
$(".message").hide();
$("#login_form").submit(function()
{
$(".login-form").hide();
$(".loading").fadeIn(200);

$.post("login.php",{ uname:$('#username').val(),pword:$('#password').val()} ,function(data)
{
$(".loading").hide();

if(data == '1')
{
$('.message').html('<p>Success - Redirecting...</p>');
window.location.replace("?secure-page");
}
else
{
$('.message').html('<p>Login Failed - Please Try Again</p>');
$(".login-form").fadeIn("slow");
}
$(".message").fadeIn("slow").delay(1000).fadeOut(1000);
});
return false;
});
});
</script>
<style type="text/css">
<!--
.login {
background-color: #9CF;
border-right-width: medium;
border-bottom-width: medium;
border-left-width: medium;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-right-color: #333;
border-bottom-color: #333;
border-left-color: #333;
text-align: center;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
color: #333;
padding: 10px;
margin-top: 100px;
margin-right: auto;
margin-left: auto;
border-top-width: medium;
border-top-style: solid;
border-top-color: #333;
width: 270px;
}
.message {
color: #333;
background-color: #F60;
border: 1px solid #F00;
padding: 2px;
font-family: Arial, Helvetica, sans-serif;
}
input {
border: 1px solid #333;
padding: 2px;
}
.top {
font-family: Arial, Helvetica, sans-serif;
color: #333;
background-color: #CCC;
position: absolute;
left: 5px;
top: 5px;
width: 746px;
}
-->
</style>



</head>
<body>


<div class="login">
<div class="message"></div>
<div class="loading"><img src="loading.gif" width="100" height="100" /></div>
<div class="login-form">
<form id="login_form" method="post" action="">
<table width="268" border="0">
<tr>
<td width="102">Username</td>
<td width="156">
<label>
<input type="text" name="username" id="username" />
</label>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<label>
<input type="password" name="password" id="password" />
</label>
</td>
</tr>
<tr>
<td>Remember me?</td>
<td align="left"><label>
<input type="checkbox" name="checkbox" id="checkbox" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="button" id="button" value="Submit" />
</label></td>
</tr>
</table>
</form>
</div>
</div>

</body>
</html>

 

Login.php

 

<?php
$db_user = "username";
$db_pass = "pw";
$db = "db_name";

mysql_connect('localhost',$db_user,$db_pass);
mysql_select_db($db);

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['pwid']));

$sql= "SELECT * FROM roster_March2010 WHERE username = '$username' AND pwid = '$password'";
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);

if ($num_rows == '1')
{
setcookie("logged_in", $username, time()+3600);
echo '1';
} else
{
echo '0';
}

?>

 

MySQL Table

CREATE TABLE `roster_March2010` (
  `name` text,
  `username` text,
  `pwid` varchar(10) default NULL,
  `user_id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

 

 

should be passing authentication, but I get the login failed message no matter what.

 

In login.php, you should have:

 

$username = mysql_real_escape_string($_POST['uname']);
$password = mysql_real_escape_string(md5($_POST['pword']));

 

As these are the field names you pass using AJAX.

 

good catch. can't believe i missed that. that's likely the problem.

still isn't working for me, for some reason. Updated code

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Ajax Login in php using jquery's fading effects</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".loading").hide();
$(".message").hide();
$("#login_form").submit(function()
{
$(".login-form").hide();
$(".loading").fadeIn(200);

$.post("login.php",{ username:$('#username').val(),pwid:$('#pwid').val()} ,function(data)
{
$(".loading").hide();

if(data == '1')
{
$('.message').html('<p>Success - Redirecting...</p>');
window.location.replace("?secure-page");
}
else
{
$('.message').html('<p>Login Failed - Please Try Again</p>');
$(".login-form").fadeIn("slow");
}
$(".message").fadeIn("slow").delay(1000).fadeOut(1000);
});
return false;
});
});
</script>
<style type="text/css">
<!--
.login {
   background-color: #9CF;
   border-right-width: medium;
   border-bottom-width: medium;
   border-left-width: medium;
   border-right-style: solid;
   border-bottom-style: solid;
   border-left-style: solid;
   border-right-color: #333;
   border-bottom-color: #333;
   border-left-color: #333;
   text-align: center;
   font-size: 14px;
   font-family: Arial, Helvetica, sans-serif;
   color: #333;
   padding: 10px;
   margin-top: 100px;
   margin-right: auto;
   margin-left: auto;
   border-top-width: medium;
   border-top-style: solid;
   border-top-color: #333;
   width: 270px;
}
.message {
   color: #333;
   background-color: #F60;
   border: 1px solid #F00;
   padding: 2px;
   font-family: Arial, Helvetica, sans-serif;
}
input {
   border: 1px solid #333;
   padding: 2px;
}
.top {
   font-family: Arial, Helvetica, sans-serif;
   color: #333;
   background-color: #CCC;
   position: absolute;
   left: 5px;
   top: 5px;
   width: 746px;
}
-->
</style>



</head>
<body>


<div class="login">
<div class="message"></div>
<div class="loading"><img src="loading.gif" width="100" height="100" /></div>
<div class="login-form">
<form id="login_form" method="post" action="">
<table width="268" border="0">
<tr>
<td width="102">Username</td>
<td width="156">
<label>
<input type="text" name="username" id="username" />
</label>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<label>
<input type="password" name="pwid" id="pwid" />
</label>
</td>
</tr>
<tr>
<td>Remember me?</td>
<td align="left"><label>
<input type="checkbox" name="checkbox" id="checkbox" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="button" id="button" value="Submit" />
</label></td>
</tr>
</table>
</form>
</div>
</div>

</body>
</html>



 

<?php
$db_user = "username";
$db_pass = "password";
$db = "DB";

mysql_connect('localhost',$db_user,$db_pass);
mysql_select_db($db);

$username = mysql_real_escape_string($_POST['username']);
$pwid = mysql_real_escape_string(md5($_POST['pwid']));

$sql= "SELECT * FROM roster_March2010 WHERE username = '$username' AND pwid = '$pwid'";
$query = mysql_query($sql) or die("Query Failed: $sql - " . mysql_error());
$num_rows = mysql_num_rows($query);

if ($num_rows == '1')
{
setcookie("logged_in", $username, time()+3600);
echo '1';
} else
{
echo '0';
}

?>

this may or may not fix the issue.

 

if ($num_rows == '1')

 

mysql_num_rows returns an int and your are checking against a string. php may understand what you're trying to do and do the conversion for you but for good coding practise, you should do:

 

if ($num_rows == 1)

yea, I meant I get the error invalid login,which is what the JQuery is producing since everything is evaluating to 0

else
{
$('.message').html('<p>Login Failed - Please Try Again</p>');
$(".login-form").fadeIn("slow");
}

 

even though should be working, I will try w/o the JQuery as you suggested.

so in your php script just do something like this after you sql call:

$sql= "SELECT * FROM roster_March2010 WHERE username = '$username' AND pwid = '$password'";
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
mail("[email protected]","ajax debug","$sql\n\n mysql num rows = $num_rows");

 

now you should get an email of the query it ran and the number of rows it returned. with this you can test it yourself on your database to make sure it actually returns a result.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.