0

ok I surrender .... I have 2 function very very easy but don't understand where is my error in php function ... This is my php function

function chkstatus(){
global $mysqli;
//$cc=array();
$sql="SELECT  * FROM online WHERE id=".$_GET['idonline'];
$result=$mysqli->query($sql);
$row=$result->fetch_array();
header('Content-type: application/json');
/*$cc[]=array('value'=>$row['busy']);
$ccstr=json_encode($cc);
echo $ccstr;*/
echo $row['busy'];
}

And my javascript function

function chkstatus(){
var iduser=$('#idop').val();    
$.ajax({        
    url: 'chat.php?action=chkstatus',
    data:{idonline:iduser},
    dataType: 'json', 
    cache: false,
    success: function(data){
        console.log(data);      
    },
    error: alert("error")
}); 
setTimeout('chkstatus();',4000);
}

I have ever the error alert, and in console I haven't nothing! Any idea where I have this error???

Fabio
  • 85
  • 1
  • 2
  • 14

2 Answers2

1

use this code in your server side code:

$row['busy'] = json_encode($row['busy']);
echo $row['busy'];

Please note first that is your function chkstatus being called when the ajax call is being made.

I hope this helps you.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
1

Nothing is executing your PHP function. Solution one, call the function:

function chkstatus(){
    global $mysqli;
    //$cc=array();
    $sql="SELECT  * FROM online WHERE id=".$_GET['idonline'];
    $result=$mysqli->query($sql);
    $row=$result->fetch_array();
    header('Content-type: application/json');
    /*$cc[]=array('value'=>$row['busy']);
    $ccstr=json_encode($cc);
    echo $ccstr;*/
    echo $row['busy'];
}
chkstatus();

Solution 2 just run the code:

//$cc=array();
$sql="SELECT  * FROM online WHERE id=".$_GET['idonline'];
$result=$mysqli->query($sql);
$row=$result->fetch_array();
header('Content-type: application/json');
/*$cc[]=array('value'=>$row['busy']);
$ccstr=json_encode($cc);
echo $ccstr;*/
echo $row['busy'];

Your code also is open to injections. How can I prevent SQL injection in PHP?

You probably also want to while loop the fetch, otherwise you're only pulling one record.

Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51