-3

I'm using vanilla JavaScript in this code. I created a variable of responseText and implement it on the if statement and even if condition A is true, it didn't work.

  setInterval(()=>{
  
  var http =new XMLHttpRequest();
  http.open("GET","send.php",true);
  http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  http.onreadystatechange= function(){
  
  if(http.readyState==4 && http.status==200){
  var pu = http.responseText;
 
 if(pu == "0"){
 
//do A

} else {
 // Do B
}

  }
  
  } 
  
  
  http.send();
  }, 1000)

The send.php file

<?php

session_start();

include"dbh.inc.php";

?>

<?php

  $sqll =  "SELECT * FROM `like-unlikes` WHERE `post_user`='".$_SESSION['username']."' AND `Stauts`=0";
 
 $queryy = mysqli_query($conn, $sqll);
 
 if($queryy){
 
 if(mysqli_num_rows($queryy) > 0){
 
echo "0";
 
 } else {
 
 echo "1";
 
 }
 }
 ?>

Even if condition A is true it didn't work.

  • Didn't worked @Muhammed – Harshit tanwar Sep 21 '21 at 06:47
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 21 '21 at 11:25

1 Answers1

0

please check through console.log() method that what data is coming. In many cases, PHP returns data other than echoed data. For removing this place the following line in start of your file inside PHP tags

ob_start();

Before the condition in which you echo your data, Place the following line

ob_end_clean();

You can see what these commands do on PHP documentation