When I do not use if condition($last_msg_id_db>$last_msg_id) in load.php file then jquery function works allright and keep checking the data after each half second.
However If i use, if condition , jquery function works only first time but after first time running, it did not work and stops working.
I think ,I am not placing the if condition in load.php at right place.Plz help Or suggest any alternative approach.
php file(Load.php) is here
$last_msg_id=$_POST['last_msg_id'];
$sql=mysqli_query($db3->connection,"SELECT * FROM chat_com ORDER by id ASC");
$sql_m=mysqli_query($db3->connection,"SELECT max(id) as maxid FROM chat_com");
$row_m=mysqli_fetch_array($sql_m);
$last_msg_id_db=$row_m['maxid'];
if($last_msg_id_db>$last_msg_id){
while($row=mysqli_fetch_array($sql)){
$textt=$row['mesg'];
$textt2="$textt<br>";
$response=array();
$response['msg']=$textt2;
$response['last_msg_id_db']=$last_msg_id_db;
$final_response[]=$response;
}
}
echo json_encode($final_response);
Jquery function is here.
var last_msg_id = 1;
function chat_com_one(id, name) {
$('#chatcom').show('fast');
(function chatcom_load_one(id, name) {
alert(last_msg_id);
$.post('load.php', {option:'chatcom_load_one', last_msg_id:last_msg_id}, function(data) {
var json = eval('(' + data + ')');
$.each(json, function(i, row) {
$("#chatcom #commid #commidwin").append(row['msg']);
last_msg_id = row['last_msg_id_db'];
});
setTimeout(chatcom_load_one(id, name), 500);
});
}(id, name));
$('#chatcom_send').click(function() {
$.post('send.php', { option:'chat_com_send_one', text:$('#chatcom_text').val(), tocom:id}, function(data) {
document.getElementById('chatcom_text').value = '';
});
});
}
It may have not worked because
$final_responsemay not have any data after second time ajax request and the condition could have failed to set values to$final_response.You may have to initialize
$final_responsein top of theifcondition,