0

I'm developing a simple guestbook and I want to update the table with all messages without refreshing the page because if someone it's writing a comment and the page refreshes the comment will be lost. So I began writing some code with ajax to update the table but I don't know how to send an array (with comment, username, date ecc) from php to ajax. In the database I have a column named "wrote" and it can be 0 (unread) or 1 (read). 1 it's when the messages it's already on the table. This is what I've done since now, maybe it's wrong

getGuest.php

<?php

include("Database.php");

$Database = new Database( "localhost", "root", "1234");
$Database->connectToServer();
$Database->connectToDatabase("test");

$result = $Database->unreadMessages();

$rows=mysql_fetch_array($result);

echo json_encode($rows);
?>

Script.js

window.onload = function(){
    interval = window.setInterval('updateGuest()',5000);
}



function updateGuest() {
    $.ajax({
        url: 'getGuest.php',
        method: 'get',
        success: on_getGuest_success,
        error: on_error
    });
}

function on_getGuest_success(data) {
    for(var i=0; i<data.length;i++) {
       // HERE I WANT TO ADD A ROW WITH ALL MESSAGE UNREAD BUT I DONT KNOW WHAT I HAVE TO DO
    }

}

function on_error() {
    //do something

}
7
  • Firstly interval = window.setInterval('updateGuest()',5000); should be var interval = window.setInterval(updateGuest,5000); Commented Jun 16, 2015 at 12:12
  • Duplicate of stackoverflow.com/questions/11201685/… ? Commented Jun 16, 2015 at 12:13
  • @mplungjan the problem is that I used encode to create the json but I don't know how to use it on ajax Commented Jun 16, 2015 at 12:15
  • the json it's correct. It's something like {"name":"steven"} how can I take the value "steven" from the json? Commented Jun 16, 2015 at 12:27
  • @mplungjan I looked at other questions and I've added, before echo($rows),header('Content-Type: application/json'); but it doesn't do anything. If I delete header(...) and I wrote something like $('.guestbook').prepend("<tr><td class=\"name\" width=\"10%\">" + data.name + "</td></tr>"); data.name value it's undefined Commented Jun 16, 2015 at 12:45

1 Answer 1

1
  1. Make sure the JSON contains an array
  2. Add headers
  3. use getJSON

Like this:

PHP

$data = array(); 
while ($row = mysql_fetch_assoc($result)) { 
  $data[] = $row; 
} 
header("content-type: application/json");
echo json_encode($data);

JS:

$(function() { // when page has loaded
  var tId = setInterval(function() { // save the tId to allow to clearTimeout if needed
    $.getJSON("getGuest.php",function(data) { // call the server using jQuery's JSON access
      $('.guestbook').empty(); // empty the container
      var rows = []; // create an array to hold the rows
      $.each(data,function(_,item) { // loop over the returned data adding rows to array
        rows.push('<tr><td class="name" width="10%">' + item.name + '</td></tr>');
     });
     $('.guestbook').html(rows.join()); // insert the array as a string
    });
  },5000); // every 5 secs
});

I would personally only return what was new since last time

Sign up to request clarification or add additional context in comments.

2 Comments

This one works perfectly! Could you explain me every line on the js? set interval it's for reaping it every 5 seconds and then?
Check the chat please :) Btw I'll check ur answer as 'the solution'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.