0

I'm trying to do a login page, but I have an issue wit ajax method:

 $("#login").click(function(){

        username=$("#user_name").val();
        password=$("#password").val();
         $.ajax({
            type: "POST",
            url: "login.php",
            data: {name : username, pwd : password},
            success: function(html){
              if( ($.trim(html)) =='true')
              {
                $("#login_form").fadeOut("normal");
                        $("#shadow").fadeOut();
                        $("#profile").html("<a href='logout.php' id='logout'>Logout</a>");

              }
              else
              {
                    $("#add_err").html("Wrong username or password");
              }
            },
            beforeSend:function()
                {
                 $("#add_err").html("Loading...")
            }
        });
         return false;
    });

login.php:

<?php  

 session_start();

 $username = $_POST['name'];
 //$username = $mysqli->real_escape_string($username);
 $password = ($_POST['pwd']);
 //$password = $mysqli->real_escape_string($password);

 $mysqli = mysqli_connect('localhost','root','','peer');
 $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
 $result = mysqli_query($mysqli,$query)or die(mysqli_error());
 $num_row = mysqli_num_rows($result);
 $row=mysqli_fetch_array($result);
 if( $num_row >=1 ) {
  echo 'true';
  $_SESSION['user_name']=$row['username'];
 }
 else{
 echo 'false';
 }
?>

I'm posting to login.php username and password, but obviously get nothing back, when I tried execute only php script it works correct. A html object seems like is still false. Do you see any mistake here ??

thanks

EDIT!

2
  • 1
    Did you try just echoing back the username and password to check that you receive them ? And you can change the data in ajax to : data: {name : username, pwd : password}, and try doing this if( $.trim(html) =='true') {...
    – adeneo
    May 12, 2013 at 1:40
  • it doesn't work, I just learn to use ajax and connect to db..
    – Patrik18
    May 12, 2013 at 1:51

2 Answers 2

2

These are the issues I see here

  • your data mat not be properly encoded. Instead of data: "name="+username+"&pwd="+password, use data:{name:username,pwd:password} so that jQuery will properly encode it for you.
  • you response may have leading or trailing white spaces, so try if($.trim(html)=='true')
  • always validate/sanitize input, you should use parameterized prepared statements, or at least escape the fields.
1
  • @user1726810 from your comment on the other answer, it looks like you never even reach login.php
    – Musa
    May 12, 2013 at 2:30
1

Try this:

$("#add_err").html("Loading...");
$.post('login.php',
    {'username': username, 'pwd': password},
    function(html){
        $("#login_form").fadeOut("normal");
        $("#shadow").fadeOut();
        $("#profile").html("<a href='logout.php' id='logout'>Logout</a>");
    }
);

@Musa pointed out that you shouldn't pass POST variables with

"name="+username+"&pwd="+password, use data:{name:username,pwd:password}

and he's 100% correct.

Another trick that might help is to use:

console.log(variable);

Whatever variable you pass will be returned to the console. You can view the console by pressing F12 in whatever browser your using. You could pass username and password before the ajax call is made, that way you can see if you're actually getting the user's input. You could also console.log the html variable, that would show if you're getting a response from the server.

6
  • it doesn't work and console.log(html); get me Object {auth: false}
    – Patrik18
    May 12, 2013 at 2:07
  • I just edited the script, replaced password with pwd. What might be the problem is using $mysqli->real_escape_string. I feel like md5 would eliminate any malicious code (I could be wrong). Try it without the real_escape_string. May 12, 2013 at 2:16
  • You could also use console.log even further. Commenting out PHP code step by step may help you. Only echoing one variable at a time helps me a lot, I would start with $result, and then $num_row, and then $row May 12, 2013 at 2:18
  • I just edit my current version and how can I console.log() php variable ??
    – Patrik18
    May 12, 2013 at 2:23
  • Let's say (in the PHP file) you only echo one variable. For example 'echo $result', you could then use (in the Javascript) console.log(html), that way you'll be able to see the what the server says is $result May 12, 2013 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.