Jump to content

Shoutbox Help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
thedayismine

thedayismine

    Newbie

  • Members
  • Pip
  • 3 posts
Hello everyone, I followed a tutorial on biorust to create shoutbox's (biorust.com/tutorials/detail/110/us) . After uploading the stuff, I added some css to make it look a bit prettier and a bit of javascript to optomize the way it functions, and I'm stuck on two things regarding php and javascript- (I'm not very php / javascript smart).

I want the page to refresh every 3-5 seconds, so I tried the
<meta http-equiv="refresh" content="5">
but I don't want whatever the person was typing in the 'message' section to disappear on refresh. I just want the table td holding the already posted shouts to refresh but not the actual form- I searched and searched google for this and all I got is that it's possible with AJAX- but I didn't find a clear- working code

I also want the 'name' to be remembered in a javascript or php cookie, but I can't get any of the codes I searched for to work with the script =/

I hope I'm not asking for too much- If you know how to resolve ANY of the above two issues please help me out =/

the currently revised shout.php script is


<?php


// calling session_start() the function which starts our authentication session

session_start();


// connecting to mysql server

$l = mysql_connect  ( "localhost" , "Admin" , "shoutbox" ) or die("Error connecting:<BR><BR>".mysql_error());

mysql_select_db( "evergre7_shoutbox" ) or die("Error getting db:<BR><BR>".mysql_error());


// defining getShouts() which is our function that gets all of our shouts

function getShouts()

{


	echo '<div align="center">

		<table width="150" cellspacing="3" cellpadding="0" class="table">

		  <tr>

			<td> 

	';	

	

	$query = mysql_query("SELECT * FROM shouts ORDER BY id DESC LIMIT 10") or die(mysql_error());

	while ($row = mysql_fetch_array($query) )

	{

		

		$name = stripslashes($row['Name']);

		$contact = stripslashes($row['Contact']);

		$shout = stripslashes($row['Shout']);

		

		if(empty($contact))

		{

		

			echo '<p><span class="author">'.$name.'</span> <span class="dash">|</span> <span class="shout">'.$shout.'</span></p>';

			

		} else {

		

			echo '<p><span class="author"><a href="'.$contact.'" target="_blank">'.$name.'</a></span> - <span class="shout">'.$shout.'</span></p>';


		} // if empty contact

				

	} // while row mysqlfetcharray query

	

	echo '<br><br>';

	

	echo '

			</td>

		  </tr>


		';	


} // function getshouts



// our processing if control statement

if ( isset ( $_POST['shout'] ) )

{

	

	$name = addslashes($_POST['name']);

	$message =  $_POST['message'];

	

	if ( ( isset($name) ) && ( isset($message) ) )

	{

		

		// getting smilie list

		$smilies = mysql_query("SELECT * FROM smilies") or die(mysql_error());

		while($get = mysql_fetch_array ($smilies))

		{

			

			$alt = $get['Alt'];

			$smilie = $get['URL'];

			

			$message = str_replace( $get['Symbol'] , '<img src="smilies/'.$smilie.'" border="0" width="15" height="15" alt="'.$alt.'">' , $message);

			$themessage = addslashes($message);

			

			// replacing all smilies

					

		}

	

		mysql_query("INSERT INTO shouts (Name, Contact, Shout) VALUES ( '$name' , '$contact' , '$message' )") or die(mysql_error());

		$_SESSION['has_posted'] = 'yes';

		header("Location: shout.php");

		

		// if required fields aren't empty, process into database

		

	} else {

	

		echo '<script>alert("Some fields were not filled out!");</script>';

		header("Location: shout.php");

		

		// if required fields were left empty, show an error dialog

		

	}


}/* else {


	echo '<script>alert("Please follow the form to this page.");</script>';

	header("Location: shout.php");

	

	// if they weren't even referred from the form, show error dialog and redirect


} // if isset post shout


/* STARTING THE MAIN SCRIPT NOW */


// starting the table


//displaying the shouts

getShouts();



mysql_close($l);



?>

	  

			<div align="center">

			<form name="shout" method="post" action="shout.php" class="form">

				<tr><td>

					<span class="headers">name:</span> <br><input name="name" type="text" id="name" size="25" maxlength="10" onfocus="this.select()" onblur="this.value=!this.value?'Anonymous:':this.value;" value="Anonymous" class="input"><br>

				</tr></td><tr><td>

					<span class="headers">Message:</span> <br><input name="message" type="text" id="message" size="25" class="input"><br>

				</tr></td><tr><td>

					<input name="shout" type="submit" id="shout" value="Post!" class="submit">

				</tr></td>

			  </div>

			</form>

			

		  

		  </table>

		  </div>


<link href="shoutbox.css" rel="stylesheet" type="text/css">


<body>


<script type="text/javascript">

window.onload = function () {

		// focus on the input field for easy access...

		var input = document.getElementById ('message');

		input.focus ();

		// ...but if someone wishes to go back in their history, let them!

		document.onkeydown = function (e) {

			if (!e) {

				var e = window.event;

			}

			if (e.keyCode === 8 && !input.value) {

				history.back();

			}

		};

	};


</script>



#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
If I have understood shouts correctly, most are using ajax to refresh the content of the shoutbox, and have the shouts separated from the message box.

if you don't want to use ajax to reload a certain part of your box, I'd recommend an iframe including the shouts so the message form won't be affected of the refreshing shouts.

that's the two ways that is obvious for me.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Also, depending on the connection and data involved, doing a refresh every 3-5 seconds may not even be possible. With a slow/laggy connection, it can easily take 10 seconds just for the page to load, and a 3-5 second refresh would mean I can't even read what's on-screen.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
thedayismine

thedayismine

    Newbie

  • Members
  • Pip
  • 3 posts
hmm.. okay instead of 3-5 seconds is there an easy way for the page to refresh whenever there is a new shout?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That would require AJAX.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
thedayismine

thedayismine

    Newbie

  • Members
  • Pip
  • 3 posts
and does AJAX = rewriting the script, or is there a copy and paste solution?

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
That would mean both rewriting and splitting it into two scripts, but that is needed anyhow with those changes you want.