0

I am working on php and javascript project. I just want to compare echo return value of php with javascript variable.

php backend code which returns 'no' using echo

 if($connection){
    $sql ="SELECT secondname FROM account WHERE email = '$email' && password = '$password'";
    $searchquery = mysqli_query($connection, $sql);
    if(!(mysqli_num_rows($searchquery) == 0)) {
        $row = mysqli_fetch_array($searchquery);
        $secondname = $row['secondname'];
        echo $secondname ;
    } else {
       echo 'no';

    }

Now comparing with javascript variable

$.post("signnin.php",{
               email: email,
               password: password,
           },
        function(data, status){
        if(data == 'no'){
          console.log('same');

        }else{
          console.log('not same');

        }
        });

it give same result if value are same or not. i also JSON.stringify but it still not working

3
  • 4
    So check what the value of data actually contains..? I would assume that the problem is because you're returning a plain text response and there's some whitespace you need to remove with trim(), eg if (data.trim() === 'no'). This is why it's always a better idea to return a serialised response. Commented Apr 17, 2020 at 11:46
  • 2
    You should use standard SQL AND, not &&. && is non-standard SQL and is deprecated in MySQL 8 and will be removed in a future version. Commented Apr 17, 2020 at 11:50
  • 2
    Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. Commented Apr 17, 2020 at 11:50

2 Answers 2

1

The output from the URL probably includes white space.

JSON is a good way to normalize that, but you need to apply it at the PHP end, not the JavaScript end.

    $secondname = $row['secondname'];
    header("Content-Type: application/json");
    echo json_encode([ "secondname" => $secondname ]);
 } else {
    header("Content-Type: application/json");
    echo json_encode([ "failure" => "Login failed" ]);
 }

and then:

    $.post(
        "signnin.php",
        { email, password },
        function(data, status){
            if(data.failure){
                console.log('same');
            } else {
                console.log('not same');
            }
        }
    );
Sign up to request clarification or add additional context in comments.

Comments

0

You are probably sending whitespaces in your PHP script. There are 2 ways to solve this -

  1. Ensure that you put the starting php tag from the very first line and don't put the ending php tag (?>).

  2. Instead of comparing texts, compare numbers. In php script echo 1 and in js compare data==1. This way whitespaces are automatically ignored.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.