Jump to content

PHP / Javascript Chat


Sesquipedalian

Recommended Posts

Hey everyone,

 

I'm trying to make a very simple PHP/Javascript Chat. So far I'm just attempting to make it be able to constantly update the messages on the page. I'm fairly new to javascript, and I only know the basics of PHP, so excuse any bad coding. :P

 

Anyways, the main thing I want is for the page to not have to refresh, so I'm trying to just use Javascript to replace a section on the page and just constantly reload the mysql data. So far I'm only doing it in terms of "Handle" (or username), to see if I could get it to work. I can't.

 

What's happening is that it is continually calling the Javascript function (with setInterval), but the PHP function just goes once and sets itself entirely in the code, never updating. Is there a way to get around this? Here is my code thus far:

 

<?
$dbhost = '----';
$dbuser = '----';
$dbpass = '----';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'chat';
mysql_select_db($dbname);

function findHandle() {
global $query;
$query['handle'] = mysql_query('SELECT handle FROM chat');
}

?>

<script>
function dispHandle(han) {
document.getElementById('handle').innerHTML = han;
}
</script>

<span id="handle"></span>
<script>
setInterval(dispHandle(<? findHandle(); echo "'"; 
						for ($i = mysql_num_rows($query['handle'])-1; $i >= 0; $i--) {
							echo mysql_result($query['handle'], $i).'<br />';
						}
						echo "'";
					?>), 100);
</script>

 

Any help is appreciated. What I'm really looking for is for the PHP to be able to refresh as well so it can find the new results in my mysql database. I'm trying not to do make the page refresh, so please help me if that is possible.

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/111609-php-javascript-chat/
Share on other sites

AJAX stands for "Asynchronous Javascript And XML".

 

Javascript sends a request (in the background) to the server (like a normal page would).  The server picks it up and handles it in the appropriate language (ie: server running PHP obviously handles PHP code).  Server outputs results and sends them back.  Javascript listens for that return text(or XML) and handles it accordingly (display on the page that requested new info, or just make database updates)

 

So basically "no".  The Javascript part of AJAX sends request to the server, and the server interfaces with mysql and returns the results if any.

Link to comment
https://forums.phpfreaks.com/topic/111609-php-javascript-chat/#findComment-572927
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.