SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJJAX PHP Login form

    Hi all,

    I have a login form i want to be ajaxed only issue is im getting login failed this is my ajax and html form its as below

    HTML Code:
    <script language="javascript">
    //  Developed by Roshan Bhattarai 
    //  Visit http://roshanbh.com.np for this script and more.
    //  This notice MUST stay intact for legal use
    
    $(document).ready(function()
    {
    	$("#login_form").submit(function()
    	{
    		//remove all the class add the messagebox classes and start fading
    		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
    		//check the username exists or not from ajax
    		$.post("alogin.php",{ user_name:$('#username').val(),password:$('#password').val()
    	} ,function(data)
            {
    		  if(data=='yes') //if correct login detail
    		  {
    		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
    			{ 
    			  //add message and change the class of the box and start fading
    			  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
    			  { 
    			  	 //redirect to secure page
    				 document.location='secure.php';
    			  });
    			  
    			});
    		  }
    		  else 
    		  {
    		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
    			{ 
    			  //add message and change the class of the box and start fading
    			  $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
    			});		
              }
    				
            });
     		return false; //not to post the  form physically
    	});
    	//now call the ajax also focus move from 
    	$("#password").blur(function()
    	{
    		$("#login_form").trigger('submit');
    	});
    });
    </script>
    <!-- Show Message for AJAX response -->
    <form method="post" action="" id="login_form">
    <div align="center">
    <div style="color:#FFF;">
       User Name : <input name="username" type="text" id="username" value="" maxlength="20" />
    </div>
    <div style="margin-top:5px;color:#FFF;">
       Password :
        &nbsp;&nbsp;
        <input name="password" type="password" id="password" value="" maxlength="20" />  
    </div>
    <div class="buttondiv" style="color:#FFF;">
        <input name="Submit" type="submit"  id="submit" value="Login" style="margin-left:-10px; height:23px" style="color:#FFF;"  />
        <span id="msgbox" style="display:none"  style="color:#FFF;"></span>
    </div>
    </div>
    below is my php page which as follows also nothing fancy just the usual login form as it shud work when i click on login but it deosnt when using ajax it is this

    PHP Code:
                    $password mysqli_real_escape_string($con,stripslashes(trim($_POST['password'])));
                    
    $cQuery="SELECT * FROM admin WHERE username='".$username."' AND password='".$password."'";
                    echo 
    $cQuery;
                    
    $con;
                    
    $rs=mysqli_query($con,$cQuery);
                    if(!
    $rs)
                    {
                        echo 
    "Unable to excute the query:".mysqli_error($con);
                    }
                    else
                    {
                        
    $count=$rs->num_rows;
                        if(
    $count>0)
                        {
                            if(
    $data['password']=$password)
                            {
                                echo 
    "yes";
                                
    //now set the session from here if needed
                                
    $_SESSION['u_name']=$username
                            }
                            else
                            {
                                echo 
    "Password does not match";    
                            }
                            
                        }
                        else
                        {
                            echo 
    "Username doesnt exist";
                        }
                    } 
    Why do i get the text login stinks what am i doing wrong when i know for sure my php code and login info are correct?

    i was using a tutorial and this is what the ajax/jquery tutorial said to do not sure what to do now though

    any help would be good what am i doing wrong?

    Thanks,William

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What i really want to know is why your mixing MySQLi OOP code with procedural code eg.

    mysqli_query
    $rs->num_row

    To me this looks like where all the problems start.

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well it seems to work ive done it many times b4 anyway ive adjusted the login code its

    PHP Code:
    include("dbconnect.php");
    $username mysqli_real_escape_string($con,stripslashes(trim($_POST['username'])));
                    
    $password mysqli_real_escape_string($con,stripslashes(trim($_POST['password'])));
                    
    $cQuery="SELECT * FROM admin WHERE username='".$username."' AND password='".$password."'";
                    echo 
    $cQuery;
                    
    $con;
                    
    $rs=mysqli_query($con,$cQuery);
                    if(!
    $rs)
                    {
                        return 
    "Unable to excute the query:".mysqli_error($con);
                    }
                    else
                    {
                        
    $count=mysqli_num_rows($rs);
                        
    //$count=$rs->num_rows;
                        
    echo $count;
                        if(
    $count>0)
                        {
                            
                            while(
    $data=mysqli_fetch_assoc($rs))
                            {
                                if(
    $data['password']=$password)
                                {
                                    return 
    "yes";
                                    
    //now set the session from here if needed
                                    
    $_SESSION['user']=$username
                                }
                                else
                                {
                                    return 
    "Password does not match";    
                                }
                            }
                        }
                        else
                        {
                            return 
    "Username doesnt exist";
                        }
                    } 
    But how can i fix my issue i am having??

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In your javascript code before

    Code JavaScript:
    if (data=='yes')

    alert the message for data and see what response you get as it will help you debug were the error is. Also a good thing to try is returning the vital PHP variables back with a string to see what the row counts are etc.


Tags for this Thread

Bookmarks

Posting Permissions

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