Jump to content
xisto Community
Sign in to follow this  
8ennett

More Advanced Shoutbox A more advanced shoutbox with fewer files and no refresh needed

Recommended Posts

First of all you will need to create the following documents and copy and paste the contents of the below code in to them:

 

shoutbox.php


mysql_select_db ('DATABASE NAME');

 

if (isset($_GET['sendshout']) && !isset($_GET['readshout'])){

$shout = mysql_real_escape_string(trim(htmlentities(strip_tags($_POST['message']))));

mysql_query("INSERT INTO shoutbox (user, date, content, name) VALUES ('".$_SESSION['id']."', '".time()."', '".$shout."', '".$_SESSION['username']."')");

}

if (isset($_GET['readshout']) && !isset($_GET['sendshout'])){

$query = mysql_query("SELECT * FROM shoutbox ORDER BY date DESC LIMIT 0, 10");

while($row = mysql_fetch_assoc($query)){

echo "<strong>".$row['name']." linenums:0'><?php session_start(); mysql_connect ('MYSQL HOST', 'MYSQL USERNAME', 'MYSQL PASSWORD'); mysql_select_db ('DATABASE NAME'); if (isset($_GET['sendshout']) && !isset($_GET['readshout'])){ $shout = mysql_real_escape_string(trim(htmlentities(strip_tags($_POST['message'])))); mysql_query("INSERT INTO shoutbox (user, date, content, name) VALUES ('".$_SESSION['id']."', '".time()."', '".$shout."', '".$_SESSION['username']."')"); } if (isset($_GET['readshout']) && !isset($_GET['sendshout'])){ $query = mysql_query("SELECT * FROM shoutbox ORDER BY date DESC LIMIT 0, 10"); while($row = mysql_fetch_assoc($query)){ echo "<strong>".$row['name'].":</strong> ".stripslashes($row['content']);."<br />"; } } mysql_close ('MYSQL HOST', 'MYSQL USERNAME', 'MYSQL PASSWORD'); ?>

shoutbox.js


return;

}

htmlRequest.open('POST', 'shoutbox.php?l=sendshout=true', true);

htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

htmlRequest.send('message='+document.shoutbox.message.value);

 

document.shoutbox.message.value = '';

document.shoutbox.message.focus();

 

} linenums:0'>function ajaxFunction(){ var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ //browsers all not support, rare case alert("Your browser does not support ajax, please use Mozilla Firefox!"); return false; } } } return ajaxRequest; } function showData() { htmlRequest = ajaxFunction(); if (htmlRequest==null){ alert ("Browser does not support HTTP Request"); return; } htmlRequest.onreadystatechange = function(){ if(htmlRequest.readyState == 4){ document.getElementById("shoutarea").innerHTML = htmlRequest.responseText; } } htmlRequest.open("GET", "shoutbox.php?readshout=true", true); htmlRequest.send(null); } showData(); setInterval("showData()",1000); function saveData(){ htmlRequest = ajaxFunction(); if (htmlRequest==null){ alert ("Browser does not support HTTP Request"); return; } if(document.shoutbox.message.value == "" || document.shoutbox.message.value == "NULL"){ alert('You need to type a message!'); return; } htmlRequest.open('POST', 'shoutbox.php?l=sendshout=true', true); htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); htmlRequest.send('message='+document.shoutbox.message.value); document.shoutbox.message.value = ''; document.shoutbox.message.focus(); }

index.html

<html>    <head>    <script language="JavaScript" type="text/javascript" src="shoutbox.js"></script>    <title>Shoutbox</title>    </head>    <body>    <form id="shoutbox" name="shoutbox" method="POST" onSubmit="saveData(); return false;">    <div id="shoutarea"></div><br /><input $id="message" name="message" type="text" size="50" maxlength="100" class="textbox" /><input type="submit" name="submit" value="submit"/>    </form>    </body>  </html>

You will also need to run the following MySQL query either in the console or using phpMyAdmin:

 

CREATE TABLE `shoutbox` (

`id` integer(255) NOT NULL auto_increment,

`user` integer(255) NOT NULL,

`date` integer(255) NOT NULL,

`content` varchar(100) NOT NULL,

`name` varchar(45) NOT NULL,

PRIMARY KEY (`id`)

);

 

Now the above files assume that you have already written to your session the following variables,

$_SESSION['id'] : This is the current users unique id number

£_SESSION['username'] : The current users display name

 

These variables would usually be set in some kind of user login script however to test the shoutbox you could just write them in to the shoutbox.php script.

 

Now what happens above, the ajax javascript file contains all the custom functions required to operate the shoutbox. We include this in the head of the our html (or php) index file soit can be used when submitting our form. The forms onSubmit function being used to call the saveData function from the javascript document. I think the rest is actually pretty self-explanatory so have fun testing it out.

 

Just to recap quickly, in shoutbox.php where there is the following line:


mysql_select_db ('DATABASE NAME'); linenums:0'>mysql_connect ('MYSQL HOST', 'MYSQL USERNAME', 'MYSQL PASSWORD'); mysql_select_db ('DATABASE NAME');

Replace MYSQL HOST with your hostname eg. localhost, MYSQLUSERNAME with your username eg. root, and MYSQL...well you get the idea. Also do the same for the very last line of code in shoutbox.php. Also replace the two $_SESSION variables outlined above with what ever your site uses.

Share this post


Link to post
Share on other sites

if you can fix the shourbox issue in Xisto.com then let opaque know about it. it is broken and needs some attention. if you can help with that for Xisto then that will be good. you can sign up at Xisto and earn mycents there as well. so see how shoutbox is broken and check if you can fix that. we seriously need fix for that.

Share this post


Link to post
Share on other sites

I'm a bit too busy to be signing up for any more forums at the moment, my site needs to be up and running within 6 months and it's a lot of work for just one person. In fact I've just finished perfecting this shoutbox, included smileys, different chat rooms with one script, alerts when people enter or leave the chat and even whispering to others inside the chat window. Anyway, without being able to see the full structure of the shoutbox (eg. PHP) I doubt I could offer any advice on fixing it anyway. I'm sure opaque will get around to fixing it if he has the time. It's probably a problem with the query.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.