Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,290
    Thanks
    23
    Thanked 611 Times in 610 Posts
    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.

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi sunfighter

    I did the changes but it still slip to incorrect credentials

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,290
    Thanks
    23
    Thanked 611 Times in 610 Posts
    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.

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,290
    Thanks
    23
    Thanked 611 Times in 610 Posts
    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>

  • Users who have thanked sunfighter for this post:

    prash91 (11-16-2012)

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Nope it still dosent work.
    I have look for some different option.

    Thanks for the help Sunfighter


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •