CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   display login status without refreshing the page Ajax PHP MYSQL (http://www.codingforums.com/showthread.php?t=275930)

prash91 10-09-2012 09:42 PM

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

sunfighter 10-10-2012 06:55 PM

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.

prash91 10-20-2012 12:14 AM

Hi sunfighter

I did the changes but it still slip to incorrect credentials

sunfighter 10-21-2012 03:57 AM

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.

prash91 10-22-2012 11:49 PM

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.

sunfighter 10-23-2012 02:45 PM

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>


prash91 10-25-2012 12:17 PM

Nope it still dosent work.
I have look for some different option.

Thanks for the help Sunfighter


All times are GMT +1. The time now is 09:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.