Jump to content

[SOLVED] help with really simple php/mysql/ajax shoutbox....


scarhand

Recommended Posts

Hi, I am really frustrated with this, I am new to ajax so maybe Im not seeing something here that one of you gurus might.

 

The problem is not saving the data to my mysql database, that works fine, the problem is that it is not properly updating/showing the data in my DIV.

 

Here is the coding for my files:

 

 

 

shoutbox.js:

function ajaxFunction(){
var ajaxRequest;

try 
{
	ajaxRequest = new XMLHttpRequest();
} 
catch (e)
{
	try
	{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try
		{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (e)
		{
			// browser does not support
			alert("Browser does not support Shoutbox requests.");
			return false;
		}
	}

}
return ajaxRequest;
}

function showData() 
{
  htmlRequest = ajaxFunction();

  if (htmlRequest==null)
  {
    alert ("Browser does not support HTTP requests.");
    return;
  } 

htmlRequest.onreadystatechange = function()
  {
	if(htmlRequest.readyState == 4)
	{
		document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
	}
}

htmlRequest.open("GET", "sb_get.php", true);
htmlRequest.send(null);
}

showData();
setInterval("showData()", 1000);

 

 

 

shoutbox.php:

<?php
define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx);

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
?>

<html>
<head>
<title>Shoutbox</title>

<script language="JavaScript" type="text/javascript" src="shoutbox.js"></script>

</head>
<body>

<div id="shoutarea"></div>

<form name="shoutform" method="POST" onsubmit="saveData(); return false;">
Message: <input type="text" name="message" />
<br />
<input type="submit" name="submit" value="Shout it" />
</form>

</body>
</html>

 

 

 

sb_get.php:

<?php

include("sb_connect.php");

$getshout_query = "SELECT id, name, message,
          DATE_FORMAT(time, '%h:%i') as time
          FROM shoutbox
          ORDER BY id DESC LIMIT 25";
$getshout_result = mysql_query($getshout_query) or die(mysql_error());

while($row = mysql_fetch_array($getshout_result)) 
{
$name = $row['name'];
  $time = $row['time'];
  $message = $row['message'];
  
  echo "<div>($time) $name: $message</div>";	
}

?>

 

 

 

Now I'm not really sure about how the header information works in sb_get.php....maybe thats the issue.

 

Thank you for any help you provide.

Link to comment
Share on other sites

i am not gonna try your code on my server instead i'm going to give you some tips to find the error.

 

1. check if your sb_get.php gives any output if you put it in your browser url directly it should give some html output and also check out the browser source

 

2. see if something happens with your showData(); js function if you place it on a onclick

example

<button onclick="showData();">show data function call</button>

 

3. if the onclick thing worked try putting setInterval("showData()", 1000); on the onload option

<body onload="setInterval("showData()", 1000);">

 

this should be enough to solve your problem

Link to comment
Share on other sites

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.