Jump to content

Recommended Posts

I have written a shoutbox using JQuery AJAX and PHP.

 

The basic idea of the program is that using ajax, it inserts the data into the database. Then when it is retrieved, it is again with ajax bought back from the database and written to JSON format, then i use JQuery Ajax to output it to the screen.

 

The difficulty im having is that, after every 7-10 posts i get, the operation stops. I have no idea why this is.

 

It still writes the post into the database, but doesnt output it to screen.

 

Can someone examine the code and try to look for a solution.

 

To test it live go to http://www.thedesignmonkeys.co.uk/new and is on the right pane.

 

Here is the code...

 

 

shoutbox.js

$(document).ready(function(){


var id = 0;
this.id = id;

 // Functions 
function getNews() {  $.get("ajax/ajaxget.php", function(data)  { $("#news").html(data)}); } 


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>"

			);





		});



			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(); });



});

 

ajaxgetshoutbox.php

 

<?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];

}

$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.'"}';

	$fp = fopen('chat.txt', 'a');
	fwrite($fp, $data[0]."\r\n");

} 

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

echo $json;


$get = "SELECT * FROM chat";
$result = mysql_query($get);
$numrows = mysql_num_rows($result);

if($numrows == 6) {

$sql = mysql_query("DELETE FROM chat");

}

?>

 

send_message.php

<?php

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

$nick = $_POST['nick'];
$message = $_POST['message'];
$result = mysql_query("INSERT INTO chat (nick, message) values ('$nick','$message') ");

?>

Link to comment
https://forums.phpfreaks.com/topic/137673-need-advice-on-shoutbox-application/
Share on other sites

Kinda hard to look when you took it down ;)

 

A few things I wanted to point out to you:

ajaxgetshoutbox.php

There is a JSON encode function in PHP - which is really handy, especially if you decide to expand and add another field or two later on.

 

send_message.php

MySQL injection anyone? ;)

 

 

Now back to the real question, when you say the operation stops - does the php side or the js side stop?

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.