2012-12-01 26 views
0

當我不使用條件($ last_msg_id_db> $ last_msg_id)在load.php文件,然後jquery函數工作正常,並保持每半秒鐘後檢查數據。爲什麼如果條件不起作用?

但是,如果我使用,如果條件,jquery函數只有第一次運行,但第一次運行後,它沒有工作,並停止工作。

我想,我沒有把if條件放在load.php的正確位置。Plz的幫助或建議任何替代方法。

php文件(Load.php)是這裏

$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函數是在這裏。

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 = ''; 
    }); 
}); 
} 
+0

的目的是什麼,你檢查condition..what是值.. ??? –

+0

@SherinJose我正在檢查數據庫中是否收到新消息。和id如果這(這將是最後一個ID = last_msg_id_db)然後傳遞給jquery並且它的值是sat,等於jaquery var last_msg_id.If last_msg_id_db大於jquery last_msg_id那麼只有消息被提取。 –

+0

嘗試將第3行($ sql = ...)向下移動。正好在while($ row = mysqli_fetch_array($ sql)){。 – wormhit

回答

1

可能都沒有奏效,因爲$final_response可能沒有經過第二次Ajax請求的任何數據和狀態可能都未能值設置爲$final_response

您可能需要初始化的if條件頂$final_response

$final_response = array(); 
if($last_msg_id_db>$last_msg_id){ 
    ... 
    ... 
} 
echo json_encode($final_response); 
+0

非常感謝你它的工作 –

+0

我如何添加不同的顏色到新添加的郵件? –

+0

在您的Ajax腳本中,嘗試類似'$(「#chatcom #commid #commidwin」)。append(''+ row ['msg'] +'');''設置顏色。 –