Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 09:42 PM   PM User | #1
prash91
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
prash91 is an unknown quantity at this point
display login status without refreshing the page Ajax PHP MYSQL

I have following details of my code, in which i am trying to display login results in <div class="errors">
for example : if user name and passwor exist it will redirect to members.php page, if not then it will display msg "incorrect credential".
in this code the problem is msg "incorrect credential get displayed even though the username and password is correct.

database : login
table : register1
3 fields(username,password,email)
**********************************************************

Login.php
Code:
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>PHP Login with jQuery AJAX</title>
 <script type="text/javascript" src="js/jquery-1.6.2.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $("#login").click(function(){
 var username=$("#username").val();
 var password=$("#password").val();

 $.ajax({
 type:"POST",
 url:"loginpost.php",
 data:"username=" + username + "&password=" + password,
 success : function(result){

 if(result == "0")
 {
 $(document.location = "members.php"); 
}else
 {
 $(".errors").html("incorrect credential");
 }

 }

 });
 return false;
 }); 
});


 </script>
 </head>

 <body>
 <p>&nbsp;</p>
 <div id="content">
 <h1>Login Form</h1>
 <form action="" method="post">
 <p>
 <label for="username">Username: </label>
 <input type="text" name="username" id="username" />
 </p>
 <p>
 <label for="password">Password: </label>
 <input type="password" name="password" id="password" />
 </p>
 <p>
 <input type="submit" id="login" name="login" />
 </p>
 </form>
 <div class="errors">
 Press Login
 </div>
 </div>
 </body>
 </html>
**********************************************************

Loginpost.php

Code:
 <?php 

include_once "conndb1.php";
 if($_POST)
 {
 $username = ($_REQUEST['username']);
 $password = ($_REQUEST['password']);
 $sql=mysql_query("select * from register1 where username='$username' AND password='$password'");

 if(mysql_num_rows($sql)>0)
 {
 echo "0";
 while($info=mysql_fetch_assoc($sql))
 {
 $username = $info['username']; 
$email = $info['email']; 
$expire=time()+ 86400;
 setcookie("username",$username,$expire);
 setcookie("email",$email,$expire);
 }
 }
 }

 ?>
**********************************************************

conndb1.php
Code:
 <?php 

// Place db host name. Sometimes "localhost" but 
// sometimes looks like this: >> ???mysql??.someserver.net 
$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = "boomboom"; 
// Place the name for the MySQL database here 
$db_name = "login"; 

// Run the actual connection here 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
 mysql_select_db("$db_name") or die ("no database"); 
?>
**********************************************************
please give me some suggestion

Regards
Prash
prash91 is offline   Reply With Quote
Old 10-10-2012, 06:55 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,387
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
I can't get this to malfunction. Your php echos zero if your query works. Almost fool proof. But may I suggest some changes:
You are not encoding the $password, you should.
your query asks for everything done by the use of * and you don't need everything, you just need email! so make this change:
Code:
 $sql=mysql_query("select email from register1 where username='$username' AND password='$password'");
I really hate the if statement of mysql_num_rows($sql)>0 You will get this even if you have two or more querys that meet the curriculum, that means double entree.
Use:
Code:
if(mysql_num_rows($sql) == 1)
That will eliminate the need for the while loop replace it with:
Code:
$info=mysql_fetch_assoc($sql);
You don't need the $username = $info['username']; line because you already have $username.

Now the echo "0"; This is just MHO but I like something more substantial like echo "correct"; and use an else statement to echo "incorrect credential";

for your html ajax return Id change things to:
Code:
if(result == "correct")
{
  $(document.location = "members.php");
}else
{
  $(".errors").html("incorrect credential");
}
Loginpost.php:
PHP Code:
<?php
include_once "conndb1.php";
if(
$_POST)
{
$username = ($_REQUEST['username']);
$password = ($_REQUEST['password']);  // use encode of your choice here
$sql=mysql_query("select email from register1 where username='$username' AND password='$password'");
if(
mysql_num_rows($sql) == 1)
{
  echo 
"correct";
  
$info mysql_fetch_assoc($sql);
  
$email $info["email"];
  
$expire=time()+ 86400;
  
setcookie("username",$username,$expire);
  
setcookie("email",$email,$expire);
}else{
  echo 
"incorrect credential";
}
?>
PS. Make sure the db is connected.

Last edited by sunfighter; 10-10-2012 at 07:10 PM..
sunfighter is offline   Reply With Quote
Old 10-20-2012, 12:14 AM   PM User | #3
prash91
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
prash91 is an unknown quantity at this point
Hi sunfighter

I did the changes but it still slip to incorrect credentials
prash91 is offline   Reply With Quote
Old 10-21-2012, 03:57 AM   PM User | #4
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,387
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Use this line instead to see query errors:
Code:
$sql=mysql_query("select email from register1 where username='$username' AND password='$password'") or die(mysql_error());
Use phpMyAdmin to look at the database to see if the name and pass your using is there. Use phpMyAdmin to see if you only have one entree with that data.
sunfighter is offline   Reply With Quote
Old 10-22-2012, 11:49 PM   PM User | #5
prash91
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
prash91 is an unknown quantity at this point
I have done the following changes

Login.php
Code:
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>PHP Login with jQuery AJAX</title>
 <script type="text/javascript" src="js/jquery-1.6.2.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $("#login").click(function(){
 var username=$("#username").val();
 var password=$("#password").val();

 $.ajax({
 type:"POST",
 url:"loginpost.php",
 data:"username=" + username + "&password=" + password,
 success : function(result){

 if(result == "correct")
{
  $(document.location = "members.php");
}else
{
  $(".errors").html("incorrect credential");
}

 }

 });
 return false;
 }); 
});


 </script>
 </head>

 <body>
 <p>&nbsp;</p>
 <div id="content">
 <h1>Login Form</h1>
 <form action="" method="post">
 <p>
 <label for="username">Username: </label>
 <input type="text" name="username" id="username" />
 </p>
 <p>
 <label for="password">Password: </label>
 <input type="password" name="password" id="password" />
 </p>
 <p>
 <input type="submit" id="login" name="login" />
 </p>
 </form>
 <div class="errors">
 Press Login
 </div>
 </div>
 </body>
 </html>

Loginpost.php

Code:
<?php
include_once "conndb1.php";
if($_POST)
{
$username = ($_REQUEST['username']);
$password = md5($_REQUEST['password']);  // use encode of your choice here
$sql=mysql_query("select email from register1 where username='$username' AND password='$password'") or die(mysql_error());
if(mysql_num_rows($sql) == 1)
{
  echo "correct";
  $info = mysql_fetch_assoc($sql);
  $email = $info["email"];
  $expire=time()+ 86400;
  setcookie("username",$username,$expire);
  setcookie("email",$email,$expire);
}else{
  echo "incorrect credential";
}
}
?>
It still dosen't work it slips to incorrect credential.I have also check the entries in database.
prash91 is offline   Reply With Quote
Old 10-23-2012, 02:45 PM   PM User | #6
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,387
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Now that you have added md5 encoding make sure your DB reflects this.
I see you are trying to go to another page with $(document.location = "members.php");
I didn't enclose that, but I did fix the jq ajax:
Use this
Code:
<script type="text/javascript">
$(document).ready(function() {
	$("#login").click(function(){
		var username=$("#username").val();
		var password=$("#password").val();
	    $.post("loginpost.php",
	    {
	      username:username,
	      password:password
	    },
	    function(data,status){
	      $('.errors').html(data);
	    });
	});
});
</script>
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
prash91 (11-16-2012)
Old 10-25-2012, 12:17 PM   PM User | #7
prash91
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
prash91 is an unknown quantity at this point
Nope it still dosent work.
I have look for some different option.

Thanks for the help Sunfighter
prash91 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:05 AM.


Advertisement
Log in to turn off these ads.