View Single Post
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