PHP-Code:
$(document).ready(function() {
$('#btnSend').click( function() {
sendChatText();
});
startChat();
});
function startChat(){
setInterval( function() { getChatText(); }, 2000);
}
function getChatText() {
$.ajax({
type: "GET",
url: "refresh.php?lastTimeID=123456
}).done( function( data )
{
var jsonData = JSON.parse(data);
var jsonLength = jsonData.results.length;
var html = "";
for (var i = 0; i < jsonLength; i++) {
var result = jsonData.results[i];
html += '<div><b>' + result.usrname +'</b>: ' + result.chattext + '</div>';
lastTimeID = result.id;
}
alert(html);
$('#chatwindow').append(html);
});
}
function sendChatText(){
var chatInput = $('#chatmsg').val();
var nick = $('#chatnick').val();
if(chatInput != ""){
$.ajax({
type: "GET",
url: "index.php?chattext=" + encodeURIComponent( chatInput ) + "&chatnick=" + encodeURIComponent( nick )
});
}
}
//die php funktion sieht so aus:
public function get_Chat() {
$maxlines = 50;
$arr = Array();
$line = Array();
$jsonData = '{"results":[';
$result_to_read = $this -> selectSimpleSql('webchat_lines', 'room', $this -> room, '', '', 'id', 'ASC');
$num_rows = $result_to_read -> num_rows;
while($chatFeed = $result_to_read ->fetch_assoc()) {
if ($num_rows > $maxlines)
$this -> db -> query("DELETE FROM webchat_lines WHERE id = '".$this -> get_min_id()."'");
$line[] = $chatFeed['author'];
$line[] = $chatFeed['text'];
$arr[] = json_encode($line);
}
$jsonData .= implode(",", $arr);
$jsonData .= ']}';
return $jsonData;
}
{"results":[["Herr xyz",""],["Herr xyz","","Herr xyz:","Guten Tag, Herr xyz wird Sie in K\u00fcrze beraten, geben Sie uns eine Minute Zeit, Sie zu kontaktieren..."]]}
wie könnte ich das Script zum laufen bringen?
Kommentar