Sesquipedalian Posted June 24, 2008 Share Posted June 24, 2008 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. 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 More sharing options...
trq Posted June 24, 2008 Share Posted June 24, 2008 PHP runs server side while javascript runs client side. hence, you cannot simply execute php via javascript. You'll want to find some tutorials on Ajax. Link to comment https://forums.phpfreaks.com/topic/111609-php-javascript-chat/#findComment-572905 Share on other sites More sharing options...
Sesquipedalian Posted June 24, 2008 Author Share Posted June 24, 2008 Alright, thanks for pointing me in the right direction. - Adam Link to comment https://forums.phpfreaks.com/topic/111609-php-javascript-chat/#findComment-572910 Share on other sites More sharing options...
Sesquipedalian Posted June 24, 2008 Author Share Posted June 24, 2008 Sorry, one more question.. is it possible to connect to MySQL with AJAX only? When I search google all of the results for MySQL with AJAX need PHP implementation. Would using PHP in this case still make there be a need to refresh? Thanks, - Adam Link to comment https://forums.phpfreaks.com/topic/111609-php-javascript-chat/#findComment-572916 Share on other sites More sharing options...
xtopolis Posted June 24, 2008 Share Posted June 24, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.