1

I have an ajax request that looks like this

$(document).ready(function() {
    $(document).on('click', '#submit', function() {
        var UserName = $('#username').val();
        var PassWord = $('#password').val();
        console.log(UserName);
        $.ajax({
            type: 'POST',
            url: 'ajax/Login.php',
            dataType: "text",
            data: {
                username: UserName,
                password: PassWord
            },
            success: function(data) {
                alert(JSON.stringify(data));
                window.location='pages/mainpage.php';
            },
            error: function(data) {
                alert('Login Error');
                //window.location='../index.php';
            }
        });
    });
});

and my php is like this

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

if (isset($username)) {
    $stmt = $dbh->prepare("SELECT * FROM userlist_tbl WHERE username = ? ");
    $stmt->bindValue(1, $username);
    $stmt->execute();
    $selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($selected_row['username'] === $username) {
        if ($selected_row['password'] === $password) {
            $_SESSION['login_user'] = $username;
            echo "Welcome ".$username;
        }else{
    echo "Password incorrect";
}
    }
}else{
    echo "Username is empty";
}

When i dont put anything in username i am expecting that the alert will be Username is empty same as when password is empty alert should be Password incorrect but i am getting "\r\n\" but if put some in username like John it will alert Welcome John"\r\n\" why is this happening?how to make it alert Username is empty when username is empty same with password?any idea is accepted..

guradio
  • 15,524
  • 4
  • 36
  • 57
  • Do you have anything else after those `echo`es ? Try to `die()` or `exit()` after them .. – Mihai Iorga Feb 09 '15 at 07:30
  • tried exit same result but when i try die the alert is only `""` sir @MihaiIorga – guradio Feb 09 '15 at 07:35
  • Try using trim to remove whitespace: `alert($.trim(JSON.stringify(data)));`, or better yet, return a JSON object with the message as a value. That way there can be no extra whitespace around the bounds of the response. – Rory McCrossan Feb 09 '15 at 07:42
  • FYI: You are using `mysql_real_escape_string` _in combination_ with prepared statements here, which is of course nonsense. (And you should not be using the `mysql` functions any more in any case; use `mysqli` or PDO instead.) – CBroe Feb 09 '15 at 09:33
  • ok sir which should i remove?and why is it none sense so i will remove them...@CBroe – guradio Feb 09 '15 at 09:56
  • @CBroe i see what you mean from here http://stackoverflow.com/questions/10750377/mysql-real-escape-string-with-pdo-php – guradio Feb 09 '15 at 10:02

2 Answers2

0

I change isset to !empty fixed the problem

guradio
  • 15,524
  • 4
  • 36
  • 57
  • im sorry it fix everything i just had wrong alert..when i change `isset` to `!empty` it is fixed it behaves like what i want it to.. @RoryMcCrossan what is the difference between `isset` and `!empty` – guradio Feb 09 '15 at 07:46
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. – Jan Aagaard Meier Feb 09 '15 at 08:30
  • @JanAagaardMeier sir it did fix the problem i jsut change `isset` to `!empty` maybe there is a difference between the two that is why it is working now – guradio Feb 09 '15 at 09:11
0

Try this: in ajax section, dataType: "text", change to dataType: "json", and server php code is following: it may work

  //put this function top of this page
   ob_start(); 

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
       $json="";
            if (isset($username)) {


       $stmt = $dbh->prepare("SELECT * FROM userlist_tbl WHERE username = ? ");
            $stmt->bindValue(1, $username);
            $stmt->execute();
            $selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($selected_row['username'] === $username) {
                if ($selected_row['password'] === $password) {
                    $_SESSION['login_user'] = $username;
                     $json.="Welcome ".$username;
                }else{
            $json.="Password incorrect";
        }
            }
        }else{
             $json.="Username is empty";
        }
   ob_end_clean(); 
   echo json_encode($json);

?>
Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27