Jump to content

Recommended Posts

I think you need to provide more information about what you are wanting. On the face of it, what you are asking doesn't make any sense. If I INSERT something into the database why would I need to pull that information out since I have all the info that was used to create it.

 

What I *think* you may be wanting to do is to have a user's page automatically updated with new information when records are inserted by another user or process. The answer to that is yes. And, it would be accomplished via AJAX, which you can do with jquery, other JavaScript libraries or roll your own.

 

The trick is for the AJAX call to pass a timestamp of the last time data was fetched so you can pull just the information needed.

 

EDIT: Here is a tutorial using jquery to build a shoutbox. It should point you int he right direction.

http://yensdesign.com/2009/01/create-a-shoutbox-using-php-and-ajax-jquery/

Chat's on a webserver are going to be refresh based. The only way around something like that is a program like java opening a constant connection to a server and constantly monitoring the database for updates. (Way more intensive on the server, and you'll need root access.)

 

For a website to work, you'll need to refresh the page or make the page request the ajax page every now and then.

 

I have made this system before. If you want the rest of the code PM me. However, here is the ajax refresh code:

 




function checkMessages() {
	// AJAX updater.
	var urlvars='?uid='+escape('<?php echo $_SESSION['key'];?>')+"&time="+updateTime();
	var xmlHttp2;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp2=new XMLHttpRequest();
	}
	catch (e) {  // Internet Explorer  
		try {
			xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
		xmlHttp2.onreadystatechange=function() {
		if(xmlHttp2.readyState==4) {
			var prefix = xmlHttp2.responseText.substr(0, 2);
			if (prefix=="re") {
				response=xmlHttp2.responseText.substr(2);
				document.getElementById('chatMessages').innerHTML=response;
			}

		}
		else if (xmlHttp2.readyState==2) {
			//document.getElementById('chatMessages').innerHTML=document.getElementById('chatMessages').innerHTML+'Text message is being sent now.';
		}
	}
	xmlHttp2.open("GET","getMessages.php"+urlvars,true);
	xmlHttp2.send(null);
	scrollToBottom('liveChatBox');
}
setInterval("checkMessages()", 5000);

The only way around something like that is a program like java opening a constant connection to a server and constantly monitoring the database for updates.

"Java" is NOT "JavaScript", they are two totally different technologies. "JavaScript" is all that is needed here.

 

There are plenty of tutorials out there on building a chat box using AJAX (I even linked to one). and I'm pretty sure plenty of ready-made code. No need rebuilding the wheel when one already exists.

I know the difference between Java and Javascript. I program in both.

 

Like I already said, web chats are refresh based. AJAX still relies on basically refreshing the results page (requesting it again and again). Where as the java application I was talking about would be a socket / constant connection where actions could be initiated by either side.

Yes, this can be done. I already provided a link for you to a tutorial to build pretty much exactly what you are looking for. What more information, specifically, are you wanting?

 

Like I already said, web chats are refresh based. AJAX still relies on basically refreshing the results page (requesting it again and again). Where as the java application I was talking about would be a socket / constant connection where actions could be initiated by either side.

I can't see why anyone would seriously consider using Java to implement a chat box using Java. No one in their right mind would ever install a Java app for a website just to use such a feature. The only exception would be in a corporate environment where the company wants strict control of the application. And, I'm pretty sure that''s not the case here.

I did follow the tutorial, but there is php and ajax both in there and I don't want to use all of the code, only what is relevant to what I need............like I said, i am not good at reading code and seeing what works together........I have to make it to know what it does.

I have to make it to know what it does.

 

That's why you have to follow the tutorial... You have to finish the tutorial to understand how it all works together. Your chat question will use AJAX. So that tutorial will help and you DO need to look at the ajax code.

 

As far as determining whether a user is offline or online, you could refresh the list of online peeps and update the div ID using the same concepts in that tutorial, in the code I posted (and sent to you in PM), and that everyone else has already said. If you want specific help with the code problem, I would post the code you are having specific problems with.

I did follow the tutorial, but there is php and ajax both in there and I don't want to use all of the code, only what is relevant to what I need...

 

If you really did read that tutorial you would know that you need both the PHP code and the AJAX code. In reality, AJAX is not a language but a description of using multiple web technologies to provide a more interactive experience - in this case a shoutbox. With just raw HTML (even if it is created with PHP), the user would have to refresh their page to see if there were any new messages; either manually or with a button. What we want is a way to automatically display new messages without refreshing the page. AJAX requires both a client-side technology and a server-side technology.

 

In the tutorial, the AJAX implementation used is a combination of client-side JavaScript (using the jquery framework) and server-side PHP. Basically, the javascript code will have a timed event that will make a request to the server and check if there are new messages. That request goes to the PHP script just as if the user had typed it into their browser. But, in this case, this all happens without any user interaction and is completely transparent to the user. The PHP script will check if there are new records and return the result back to the javascript code that made the request. The javascript code will then take the data returned and dynamically update the relevant data in the HTML page without any page refreshes.

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.