Jump to content

Recommended Posts

Hello everyone, i have built a shoutbox with PHP/JSON/AJAX/JQUERY.

 

It works fine for a number of comments and then just randomly stops after that no messages come up or anything!!

 

is this to much for the server and the database to handle

 

 

to try it

 

http://www.thedesignmonkeys.co.uk/new

 

scroll down to submit comment and the form will appear

 

you will soon see what i mean

 

Thanks

 

Garry

It should not be too much, but you should look at the interval you are sending them at, make sure that your PHP does not get errors trying to retrieve the data from the database.

 

Chances are your PHP is probably throwing an error when trying to retireve some data.

 

That is the real problem with AJAX, it is a pain to test/debug.

I find that it only seems to cut out when someone else is trying to post a comment.. if i keep adding comments it works fine.

 

i have turned error_reporting on so if it does error it will tell me!!

 

can someone go on it and test it for me and tell me where it lags out!! t

 

Thanks

 

Garry

I find that it only seems to cut out when someone else is trying to post a comment.. if i keep adding comments it works fine.

 

i have turned error_reporting on so if it does error it will tell me!!

 

can someone go on it and test it for me and tell me where it lags out!! t

 

Thanks

 

Garry

 

You are sort of missing the point.

 

You will not get this solved if you do not post the ajax section of your code. My bet is somewhere in that ajax page you have an error, with AJAX you will not see the or even with error reporting turned on due to the fact that the JS sends a new page request that has nothing to do with the current page you are using it on, other than the return data.

ok here it is

 

im using JQuery...

 

<?php 
// Report all PHP errors
error_reporting(E_ALL);
header("Expires: Mon, 26 Jul 1987 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");  

include ('../dbconnect/dbconnect.php');

$nick = $_GET['nick'];
$message = $_GET['message'];
$lastid = $_GET['lastid'];


if ($lastid == 0) {

	$sql = "SELECT MAX(chat_id) FROM chat";
	$res = mysql_query($sql);
	$lastid = mysql_fetch_row($res);		
	$lastid = $lastid[0];
	// Debug
	//echo "last id is ".$lastid;
}

$time_start = microtime(true);	
$sql = "SELECT * FROM chat WHERE chat_id > $lastid ";
$result = mysql_query($sql);
$time_end = microtime(true);
$time = ($time_end - $time_start);

$sql = "SELECT MAX(chat_id) FROM chat";
$res = mysql_query($sql);
$lastid = mysql_fetch_row($res);		
$lastid = $lastid[0];	


$data = array();
while ($row = mysql_fetch_array($result))
{
	$data[] ='{ "id": "'.htmlentities($row['chat_id']).'","nick":"'.htmlentities($row['nick']).'","message":"'.htmlentities($row['message']).'","time":"'.$time.'"}';
} 

$json = '{"latest":"'.$lastid.'","response":[';
$json .= implode(',',$data);
$json .= ']}';

echo $json;
?>

 

and her is the js file

 

$(document).ready(function(){


var id = 0;
this.id = id;


setInterval(function getShoutBox() { 


	$.getJSON("ajax/ajaxgetshoutbox.php?lastid=" + id, function(json){

		id = json.latest;
		$.each(json.response, function (i, item){ 

			$("#container").append(

			"<ul>" +
			"<li><h2>" + item.nick + ":</h2><p>" + item.message +"</p></li>" 
			 + "</ul>"

			);

			if(i == 0) {

			$("#sound_player").load("http://www.thedesignmonkeys.co.uk/content/chat.mp3");


			}




		});



			var divObject = document.getElementById('container');
			divObject.scrollTop = divObject.scrollHeight;

	});




},5000)	




function submitMessage(){	

        $.ajax({
            type: "POST",
            url: "ajax/send_message.php",
			complete: function() {

			$("#message").val("");
			$("#message").focus();

			}, 
			success: function() {$("#send_message").val("Post Comment");  },
			data: { nick: $('#nick').val(), message: $('#message').val() }

			});
}


$("#send_message").click(function () {submitMessage(); });



});

Looking at the ajaxgetshoutbox code I would say that you should re-work the logic. Something about that just does not seem right.

 

I would look at other shoutboxes and see what they do.

 

Also I would put some sort of filter/session deal on the send_message.php page so that a user cannot repost until x seconds have passed. Given that I could easily create a bot to flood it or flood it myself.

 

But yea. Sorry not to be more help, but I would take a look at other shoutbox scripts and see how they retrieve the other data.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.