NameSilo

AJAX'in it up!

Spaceship Spaceship
Watch

Albino

Munky DesignsEstablished Member
Impact
17
Ok, I hate that buzzword too, but im a bit stuck. can anyone explain/know any good tutorials on AJAX? nice and simple ones, all the ones I find dont go into detaill and have too much baggage attatched. pretty much what im trying to do is make my shoutbox refresh itself. so when someone clicks submit, its doesnt have to refresh the entire page. heres the shoutbox: http://www.barrelofmunkys.co.uk/Temp/shoutbox.php . nice and simple.

Heres the code for it:

Code:
<?php 
	include("inc/config.php");
	include("inc/globals.php");
	$loggedin = true;
	$Username = "Todd";
	$user_id = 6;
if (isset($_POST['submit'])){
	$shout = $_POST['shout'];
	mysql_query("INSERT INTO shoutbox VALUES(null, '$user_id', '$Username', '$shout', NOW());")or die(mysql_error());
	echo "<meta http-equiv=Refresh content=0;url=shoutbox.php>";
	}else{
$shout_result = mysql_query("SELECT * FROM shoutbox ORDER BY shout_id DESC")or die(mysql_error());
?>
<div id="shoutbox">
	<div class="area" align="center">
		<div class="title">Shoutbox</div>
		<div class="box">
			<?php while ($shout_row = mysql_fetch_assoc($shout_result)){
				echo "<div class=\"shoutuser\">".$shout_row['shout_postername'].": </div>";
				echo "<div class=\"shout\">".$shout_row['shout_body']."</div>";
				};
			?>
		</div>
		<?php if (!$loggedin){
			echo "You need to be logged in to submit a comment!<br />Login HERE, or Register HERE<br>";
		}else{ ?>
		Type your message here:
		<form action="shoutbox.php" method="post">
		<input name="shout" type="text" size="26" maxlength="100" />
		<input name="submit" type="submit" value="Submit">
		</form>
		<?php } ?>
	</div>
</div>

<style>
#shoutbox .area{
width:200px;
border:1px #000 solid;
}
#shoutbox .title{
text-align:center;
background-color:#8C8DDD;
color:#ffffff;
border-bottom:1px #000 solid;
}
#shoutbox .box{
margin:10px;
height:250px;
border:1px #000 solid;
overflow:auto;
}
#shoutbox .shouter{
font-size:13px;
}
#shoutbox .shoutuser{
font-size:12px;
text-align:left;
padding:2px;
text-decoration:underline;
}
#shoutbox .shout{
font-size:12px;
text-align:left;
padding:2px;
}
#shoutbox .commentbox{
}
</style>
<?php } ?>

its part of a complete site, so ignore the sloppy coding, most of it is just so it can be used stand alone.

any help is greatly appreciated!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
should I have posted this in the porgramming section :(

NPS for helping obviously!
 
0
•••
You can make your shoutbox refresh itself by using the "setTimeout("FunctionName()", 10000);" function, you can see it in use on my shoutbox found at http://www.hobnobdesign.com/scripts/AJAX/shout-box/

The code is as follows:

index.html
Code:
<html>
	<head>
	  <script src="shout.js"></script> 
	  <title>Include Example</title>
	</head>

  	<body style="margin:0px; padding:0px; background-color:#F3F3F7;" onLoad="HobnobRetrieve()">
		<table width="230px" height="100%" cellpadding="0" cellspacing="3px" border="0px">
			<tr>
				<td bgcolor="#FFFFFF" align="left" valign="top" width="230px" height="100%" style="border:1px solid #9B9AB3;">
					<div style="background:url(http://www.infectory.com/images/title-back.jpg); background-repeat:repeat-x; height:17px; width:100%; border-bottom:1px solid #9B9AB3;">
					  <div style="margin:2px; font:9pt Tahoma; font-weight:bold;">Shout</div>
					  
					  <div style="height:3px;"></div>
					  
					  <div style="font:8pt Tahoma; height:700px; overflow:scroll; color:#800000; margin-left:4px;" id="hobnobResult"></div>
					  
					  <center><div style="height:1px; width:98%; border-top:1px solid #D7D7E5;"></div></center>
					  
					  <div style="font:8pt Tahoma; color:#800000; margin-left:4px;"> 
					  <form id="myform" name="myForm">
						Name: <input type="text" style="width:214px; font:8pt Verdana;" id="shoutName" name="shoutName" value="" />
						  <div style="height:3px;"></div>
						Message: <textarea type="" style="width:214px; height:80px; font:8pt Verdana;" id="shoutMessage" name="shoutMessage" value=""></textarea>
						  <div style="height:3px;"></div>
					  </form>
						<div style="width:214px;" align="right"><input onClick="HobnobSubmit(); document.getElementById('myform').reset();" style="font:8pt Verdana; font-weight:normal; color:#0033CC; font-variant:small-caps;" type="submit" value="post" /></div>
					  </div>
					  
					  <div style="height:3px;"></div>
					  
					</div>
				</td>
			</tr>
		</table>
	</body>
</html>

JavaScript file (shout.js)
Code:
function HobnobSubmit() {

	http = GetXmlHttpObject();
 	http.open("POST", "shout.php", true);	
  	http.onreadystatechange = stateChanged;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  	
	http.send("sName=" + document.getElementById("shoutName").value + "&sMsg=" + document.getElementById("shoutMessage").value);
}

function stateChanged()  { 
  if (http.readyState == 4 && http.status == 200) { 
  	document.getElementById("hobnobResult").innerHTML = http.responseText;
  }
}

function HobnobRetrieve() {

	http2 = GetXmlHttpObject();
 	http2.open("GET", "shout.php", true);	
  	http2.onreadystatechange = retrieveChanged;	
	http2.send(null);
	setTimeout("HobnobRetrieve()", 10000);
}

function retrieveChanged()  { 
  if (http2.readyState == 4 && http2.status == 200) { 
  	document.getElementById("hobnobResult").innerHTML = http2.responseText;
  }
}

function GetXmlHttpObject() { 
	var objXMLHttp = null
	if (window.XMLHttpRequest) { objXMLHttp = new XMLHttpRequest() } 
	else if (window.ActiveXObject) { objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP") }
	return objXMLHttp
}

The Server Page [shout.php]
Code:
<?php $hobnob_mysql = mysql_connect("localhost", "user", "pass"); mysql_select_db("database"); ?>
<?php
	if (!empty($_POST['sName']) && !empty($_POST['sMsg'])) {
	  $strTempQuery = sprintf("INSERT INTO `table` (`u_name`, `u_date`, `u_message`) VALUES ('%s', '%s', '%s')", mysql_escape_string($_POST['sName']), date('Y-m-d'), mysql_escape_string($_POST['sMsg']));
	  mysql_query($strTempQuery);
	}
	
	$rsShoutResult = mysql_query("SELECT `u_name`, `u_message` FROM `hobnob_shout` ORDER BY `id`");
	while ($row = mysql_fetch_assoc($rsShoutResult)) {
		echo ("<span style='font:8pt Tahoma; text-decoration:underline; color:#000000;'>Name:</span> " . $row['u_name'] . "<br>");
		echo ("<span style='font:8pt Tahoma; text-decoration:underline; color:#000000;'>Message:</span> " . $row['u_message'] . "<br><br>");
	}
	mysql_free_result($rsShoutResult);
?>

I hope this helps you out.
 
Last edited:
0
•••
cheers mate, ill have a look :)
 
0
•••
Is your shoutbox script for sale?
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back