I am trying to create a private chatroom using CodeIgniter and JSON.
I would like the returned JSON data to be put in a list like <ul><li>messageinJSON</li></ul>
. This will permit me to style the users chat messages. I have tried different approaches I have seen on SO but there is no example quite like my need.
My Controller:
public function get_chats() {
$this->load->model('pchat_model');
$this->pchat_model->create_table();
echo json_encode($this->pchat_model->get_chat_after($_REQUEST["time"]));
return;
}
My JS:
var addDataToReceived = function(arrayOfData) {
arrayOfData.forEach(function(data) {
$("#received").val($("#received").val() + "
" + data[0]);
});
}
var getNewChats = function() {
$.getJSON("pchat/get_chats?time=" + time, function(data) {
addDataToReceived(data);
setTimeout(function() {
$('#received').animate({scrollTop:$('#received')[0].scrollHeight}, 3000);
}, 0);
time = data[data.length - 1][1];
});
}
UPDATE: getNewChats called in (document).ready function
$(document).ready(function() {
$("form").submit(function(evt) {
evt.preventDefault();
var data = $("#text").val();
$("#text").val('');
sendChat(data, function() {
alert("dane");
});
});
setInterval(function() {
getNewChats(0);
}, 5500);
});