0

I have created a relatively simple "ideas board" in PHP/jQuery/MySQL.

On page load the div #chat pulls the current entries in the database.

<div id="chat"> <?php $sql = "SELECT * from idea ORDER BY id DESC;"; $result = $pdo->query($sql); if($result->rowCount() > 0 && !empty($result)) { foreach ($result as $row) { $title = $row['title']; $idea = $row['idea']; echo '<span class="idea"><strong>' . $row['title'] . "</strong>&nbsp;-&nbsp;" . $row['idea'] . '&nbsp;<a class="delete" href="">[Delete]</a></span>'; } } ?> </div> 

However I'd like it to be refreshed periodically using:

<body onload="setInterval('chat.update()', 1000)"> 

The below example details how it can be done using a text file, how would I do it with querying a database (the example was found at http://css-tricks.com/jquery-php-chat/)?

//Updates the chat function updateChat() { if(!instanse){ instanse = true; $.ajax({ type: "POST", url: "process.php", data: {'function': 'update','state': state,'file': file}, dataType: "json", success: function(data) { if(data.text){ for (var i = 0; i < data.text.length; i++) { $('#chat-area').append($(" "+ data.text[i] +" ")); } } document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight; instanse = false; state = data.state; } }); } else { setTimeout(updateChat, 1500); } } 

I expect certain rows to be deleted as well, so on top of entries being appended, I'd also like some to be removed.

It should simulate a real-time conversation.

3
  • 1
    You could just set the timeout in the complete callback function of your ajax request Commented Jul 28, 2013 at 14:15
  • @roasted Could you show me an example? Commented Jul 28, 2013 at 14:19
  • css-tricks.com/examples/Chat.zip download files from here Commented Jul 28, 2013 at 14:20

2 Answers 2

2

You can use simple long polling technique to update your chat.

eg:

//Updates the chat function updateChat() { if(! instance){ instance = true; $.ajax({ type: "POST", url: "process.php", data: {function: 'update', state: state, file: file}, dataType: "json", success: function(data) { if(data.text){ for (var i = 0; i < data.text.length; i++) { $('#chat-area').append($(" "+ data.text[i] +" ")); } } document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight; instance = false; state = data.state; }, complete: function(){ setTimeout( updateChat, /* Request next message */ 10000 /* ..after 10 seconds */ ); }, error: function(XMLHttpRequest, textStatus, errorThrown){ //display you error message }, timeout: 10000 //Timeout is necessary to prevent chaining of unsuccessful ajax request }); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use socket.io library to implement server to client notifications and/or real time communication in both directions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.