PHPBuilder - RIDWANK-GUESTBOOK



RSS Twitter
Snippets Bbs Discussion

RIDWANK-GUESTBOOK

by: Ridwan Karsadarma
|
June 26, 2003

Version: 1.0

Type: Full Script

Category: BBS/Discussion

License: GNU General Public License

Description: This guestbook uses only one file that includes both signing and viewing guestbook in one single page. Feature: Checking for an empty post, Checking for duplicates, Striping HTML Tags, Javascript Scrolling UP, DOWN & PAGING on viewing guestbook, Admin database, and more



FOLLOW THESE STEPS (there are 4 steps):

STEP I. Save this bellow as "guestbook.php"
===========================================
<h4><a href="guestbook.php">GUESTBOOK</a> | <a href="adm_guestbook.php">ADMIN</a></h4>
<? 
// *** CONFIGURATION VARIABLES

$dbhost="localhost";
$dbusername="xxx";
$dbpassword="xxx";
$dbname="xxx";

$conn=mysql_connect($dbhost,$dbusername,$dbpassword) or die ("can't connect to server");
@mysql_select_db($dbname) or die ("can't select database");

$allow_html="no";   /* "" or "yes" to allow HTML TAGS */


if (empty($page)){ $page="Sign"; }

// *** INSERTING DATA

if ($btsend=="Send"){
   $fpostdate=time();

   if ($allow_html=="no"){
      $fnama=strip_tags($fnama);
      $femail=strip_tags($femail);
      $fcomment=strip_tags($fcomment);
   }

   if ((!empty($fnama)) and (!empty($fcomment))){
      $double=@mysql_num_rows(@mysql_query("select * from guestbook where nama='$fnama' and email='$femail' and comment='$fcomment'"));
      if ($double>0){
         // *** DO NOTHING *** //
      } else {
         @mysql_query("insert into guestbook (id,nama,email,show_my_email,comment,postdate) values ('','$fnama','$femail','$show_my_email','$fcomment','$fpostdate')");
      }
   }
}

?>

<style><!--
BODY {font-family:verdana;font-size:8pt}
TD {font-family:verdana;font-size:8pt}
.entri {font-family:verdana;font-size:8pt;border: 1px solid #666666;}
.tombol {font-family:verdana;font-size:8pt}
.navart {color:#FF0000; text-decoration:none}
--></style>
<body bgcolor=#D5D5FF>
<?

$arpage=array("Sign","View");

echo "<table border=0 cellpadding=0 cellspacing=0 width=300>";
echo "<tr><td background='gbimg/sm_empty.gif' width=50%>&nbsp;</td>";

for ($i=0;$i<count($arpage);$i++){
   if ($page==$arpage[$i]){ $img="on"; $fc="#000000"; } else { $img="off"; $fc="#666666"; }
   echo "<td><img border=0 src='gbimg/sm".$img."_left.gif' width=12 height=20></td>\n";
   echo "<td background='gbimg/sm".$img."_mid.gif' nowrap>";
   echo "<a style='font-weight:bold;text-decoration:none;color=$fc' href='$PHP_SELF?page=$arpage[$i]'>$arpage[$i] Guestbook</a></td>";
   echo "<td>\n";
   echo "<td><img border=0 src='gbimg/sm".$img."_right.gif' width=12 height=20></td>\n";

}
echo "<td background='gbimg/sm_empty.gif' width=50%>&nbsp;</td>";
echo "</tr>";
echo "</table>";
?>

<TABLE bgcolor=#FFFFFF BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=300><TR><TD WIDTH=1  BGCOLOR=#666666><IMG SRC='gbimg/trans.gif' WIDTH=1 BORDER=0 HEIGHT=1></TD><TD>

<? if ($page=="Sign"){ ?>
<!-- (start) PAGE : Sign -->

<script language="javascript">
<!--
function jumlahKata(form)
{ with (form)
  { sisa.value = 150-fcomment.value.length;
    if (parseInt(sisa.value)<0) { sisa.value = '0'; }
    fcomment.value = fcomment.value.substr(0,150);
  }
  return;
}

function cek(form)
{ with (form)
  { if (fnama.value.length==0)
    { alert("Please fill your name");
      fnama.focus();
      return false;
    }
    if (femail.value.length==0)
    { alert("Please fill your email");
      femail.focus();
      return false;
    }
    if (fcomment.value.length==0)
    { alert("Please fill your comments");
      fcomment.focus();
      return false;
    }
  }
  return;
}
//-->
</script>

<?php

echo "<form name=pesan action='".$PHP_SELF."' method='post' onSubmit='return cek(this)'>";
echo "<table border=0 cellspacing=4 cellpadding=0 width=100%>";
echo "<tr><td align=right>Name:</td><td><input name='fnama' size=30 class=entri></td></tr>";
echo "<tr><td valign=top align=right>Email:</td><td><input name='femail' size=30 class=entri>";
echo "<br><input type='checkbox' name='show_my_email' checked value='y'>Show My Email</td></tr>";
echo "<tr><td align=right valign=top>Comments:</td><td><textarea name='fcomment' cols=30 rows=5 class=entri onKeyup='jumlahKata(document.pesan);' RAP></textarea>";
echo "<br>Max Chars : 150, Chars left : <input type='text' size=3 name=sisa maxlength=3 value='150' class=entri></td></tr>";
echo "<tr><td colspan=2 align=center>";
echo "<input type='hidden' name='page' value='View'>";
echo "<input type='submit' name='btsend' value='Send' class=tombol>&nbsp;<input type='reset' value='Clear' class=tombol>";
echo "</td></tr>";
echo "</table>";
echo "</form>";



?>

<!-- (end) PAGE : Sign -->
<? } ?>


<? if ($page=="View"){ ?>
<!-- (start) PAGE : VIEW -->

<script language="JavaScript">
<!--
function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
MM_preloadImages("gbimg/bt_up_on.gif","gbimg/bt_down_on.gif");
//-->
</script>


<!--- SCRIPT UNTUK SCROLL -->
<style><!--
#divUpControl{position:relative; width:14; left:0; top:0; z-index:1; text-align: right} 
#divDownControl{position:relative; width:14; left:0; top:0; z-index:1; text-align: right} 
#divContainer{position:relative; width:298; height:150; overflow:hidden; top:0; left:0; clip:rect(0,700,290,0); visibility:hidden} 
#divContent{position:relative; top:0; left:0} 
--></style>
<script language="JavaScript">
<!--
// begin absolutely positioned scrollable area object scripts 
function verifyCompatibleBrowser(){ 
    this.ver=navigator.appVersion 
    this.dom=document.getElementById?1:0 
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
 
    this.ns4=(document.layers && !this.dom)?1:0; 
    this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5) 
    return this 
} 
bw=new verifyCompatibleBrowser() 
 
 
var speed=50 
 
var loop, timer 
 
function ConstructObject(obj,nest){ 
    nest=(!nest) ? '':'document.'+nest+'.' 
    this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0; 
    this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0; 
    this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight 
    this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight 
    this.up=MoveAreaUp;this.down=MoveAreaDown; 
    this.MoveArea=MoveArea; this.x; this.y; 
    this.obj = obj + "Object" 
    eval(this.obj + "=this") 
    return this 
} 
function MoveArea(x,y){ 
    this.x=x;this.y=y 
    this.css.left=this.x 
    this.css.top=this.y 
} 
 
function MoveAreaDown(move){ 
	if(this.y>-this.scrollHeight+objContainer.clipHeight){ 
    this.MoveArea(0,this.y-move) 
    if(loop) setTimeout(this.obj+".down("+move+")",speed) 
	} 
} 
function MoveAreaUp(move){ 
	if(this.y<0){ 
    this.MoveArea(0,this.y-move) 
    if(loop) setTimeout(this.obj+".up("+move+")",speed) 
	} 
} 
 
function PerformScroll(speed){ 
	if(initialised){ 
		loop=true; 
		if(speed>0) objScroller.down(speed) 
		else objScroller.up(speed) 
	} 
} 
 
function CeaseScroll(){ 
    loop=false 
    if(timer) clearTimeout(timer) 
} 
var initialised; 
function InitialiseScrollableArea(){ 
    objContainer=new ConstructObject('divContainer') 
    objScroller=new ConstructObject('divContent','divContainer') 
    objScroller.MoveArea(0,0) 
    objContainer.css.visibility='visible' 
    initialised=true; 
} 
// end absolutely positioned scrollable area object scripts
//-->
</script>


<?
// *** (start) PAGING

   if (empty($start)){
      $start=0;
   }

   $recperpage=10;

   $totrec=@mysql_num_rows(@mysql_query("select id from guestbook"));

   if ($totrec>$recperpage){
      $prev=$start-$recperpage;
      $next=$start+$recperpage;
      echo "<div><b>Page : <font color=#666666>";
      if (!$start==0){
         echo "<a href='$PHP_SELF?start=$prev&page=View' class=navart></a> ";
      } else {
         echo " ";
      }
      $countlp=ceil($totrec/$recperpage);
      if ($countlp>20){ $lastpage=20; } else { $lastpage=$countlp; }
      for ($i=1; $i<=$lastpage;$i++){
         if ((($start/$recperpage)+1)==$i){
            echo $i." ";
         } else {
            $startval=($i-1)*$recperpage;
            echo "<a href='$PHP_SELF?start=$startval&page=View' class=navart>".$i."</a> ";
         }
      }
      if ($start<($totrec-$recperpage)){
         echo "<a href='$PHP_SELF?start=$next&page=View' class=navart></a>";
      } else {
         echo "";
      }
      echo "</font></b></div>";
   }

// *** (end) PAGING

$rs = mysql_query("select * from guestbook order by postdate desc limit $start,$recperpage");
$ada=@mysql_num_rows($rs);
if ($ada>0){
?>

<a 
    href="javascript:;" onMouseOver="PerformScroll(-7);MM_swapImage('content_up','','gbimg/bt_up_on.gif',1);return document.MM_returnValue" 
    onMouseOut="CeaseScroll();MM_swapImgRestore()" style='text-decoration:none;color:#FF0000'><IMG 
    alt=Up border=0 name=content_up
    src="gbimg/img_up.gif" width=12 height=16> [<i>scroll up</i>]</a>

<!-- start SCROLLER -->
        
<body onload="InitialiseScrollableArea();">
<div id="divContainer"> 
<div id="divContent">
<!-- start contents -->


<?

   echo "<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor=#DFE4CD>";
   while ($row=@mysql_fetch_array($rs)){
      echo "<tr><td bgcolor=#CCCCCC><img src='gbimg/line_hor.gif' border=0 width=4 height=1></td></tr>";
      echo "<tr><td><font color=#666600>";
      echo date('j F Y - H:i',$row[postdate]);
      echo "</font><br><b>From: ".$row[nama];
      if ($row[show_my_email]=="y"){
         echo " - (<a href='mailto:".$row[email]."' class=hitam>".$row[email]."</a>)";
      }
      echo "</b><br>".$row[comment]."</td></tr>";
   }
   echo "</table>";
?>

</div>
</div>

        
<!-- end SCROLLER -->    
<a 
    href="javascript:;" 
    onMouseOver="PerformScroll(7);MM_swapImage('content_down','','gbimg/bt_down_on.gif',1);return document.MM_returnValue" 
    onMouseOut="CeaseScroll();MM_swapImgRestore()" style='text-decoration:none;color:#FF0000'
    ><IMG 
    alt=Down border=0 name=content_down
   src="gbimg/img_down.gif"  width=12 height=16> [<i>scroll down</i>]</a>

<?
} else {
   echo "<p><b>No record(s) founds</b></p>";
}

?>

<!-- (end) PAGE : VIEW -->
<? } ?>

</TD><TD WIDTH=1 BGCOLOR=#666666><IMG SRC='gbimg/trans.gif' WIDTH=1 BORDER=0 HEIGHT=1></TD></TR>
<TR><TD COLSPAN=3 BGCOLOR=#666666><IMG SRC='gbimg/trans.gif' WIDTH=1 BORDER=0 HEIGHT=1></TD></TR>
</TABLE>

STEP II. Save this bellow script as "adm_guestbook.php"
======================================================

<h4><a href="guestbook.php">GUESTBOOK</a> | <a href="adm_guestbook.php">ADMIN</a></h4>
<?php 
$dbhost="localhost";
$dbusername="xxx";
$dbpassword="xxx";
$dbname="xxx";

$conn=mysql_connect($dbhost,$dbusername,$dbpassword) or die ("can't connect to server");
@mysql_select_db($dbname) or die ("can't select database");

?>
<style><!--
BODY {font-family:verdana;font-size:8pt}
TD {font-family:verdana;font-size:8pt}
--></style>
<?
echo "<h3>&nbsp;&#149 Admin Guestbook</h3>";

if ($btedit=="EDIT"){
   $arrday=explode("/",$fpostdate1);
   $arrtime=explode(":",$fpostdate2);
   $fpostdate=mktime($arrtime[0],$arrtime[1],0,$arrday[0],$arrday[1],$arrday[2]);
   @mysql_query("update guestbook set nama='$fnama', email='$femail', show_my_email='$fshow_my_email', comment='$fcomment', postdate='$fpostdate' where id='$fid'");
}
if ($btdelete=="DELETE"){
   @mysql_query("delete from guestbook where id='$fid'");
   if (($sisa==1) and ($page>1)){
      $page=$page-1;
   }
}

// *** (start) PAGING

   if (empty($start)){
      $start=0;
   }

   $recperpage=10;

   $totrec=@mysql_num_rows(@mysql_query("select id from guestbook"));

   if ($totrec>$recperpage){
      $prev=$start-$recperpage;
      $next=$start+$recperpage;
      echo "<b>Page : </b>";
      if (!$start==0){
         echo "<a href='".$PHP_SELF."?start=".$prev."' class=navart></a>&nbsp";
      } else {
         echo "<b></b>&nbsp";
      }
      for ($i=1; $i<=ceil($totrec/$recperpage);$i++){
         if ((($start/$recperpage)+1)==$i){
            echo "<b>".$i."</b> ";
         } else {
            $startval=($i-1)*$recperpage;
            echo "<a href='".$PHP_SELF."?start=".$startval."' class=navart>".$i."</a> ";
         }
      }
      if ($start<($totrec-$recperpage)){
         echo "<a href='".$PHP_SELF."?start=".$next."' class=navart></a>";
      } else {
         echo "<b></b>";
      }
   }

// *** (end) PAGING


$t=@mysql_query("select * from guestbook order by postdate desc limit $start,$recperpage");
$ada=@mysql_num_rows($t);
if ($t>0){
   echo "<table border=0 cellspacing=1 cellpading=0 bgcolor=#CCEDFF width=100%>";
   $i=0;
   while ($row=@mysql_fetch_array($t)){
      $i++;
      if (($i%2)==0){
        $tdcol="#FFFFFF";
      } else {
        $tdcol="#CCEDFF";
      }
      if ($act==$row[id]){
         echo "<tr><td bgcolor='".$tdcol."'>";
         echo "<form action='".$PHP_SELF."' method='post'>";
         echo "<a name=view><i>Name</i> : ";
         echo "<input name='fnama' value='".$row[nama]."' class='entri'>";
         echo "<br><i>Email</i> : <input name='femail' value='".$row[email]."' class='entri'>";
         echo " <input type=checkbox name='fshow_my_email' value='y'";
         if ($row[show_my_email]=="y"){ echo " checked"; }
         echo ">show email ";
         echo "<br><i>Postdate</i> : ";
         echo "<input name='fpostdate1' value='".date('m/j/Y',$row[postdate])."' class='entri' size=10>(mm/dd/yyyy)";
         echo "<input name='fpostdate2' value='".date('H:i',$row[postdate])."' class='entri' size=5>(hh:mm)";
         echo "<br><i>Comments</i> : ";
         echo "<textarea name='fcomment' class='entri' rows=3 cols=40>".$row[comment]."</textarea>";
         echo "<input type='hidden' name='fid' value='".$row[id]."'>";
         echo "<input type='hidden' name='page' value='".$page."'>";
         // *** untuk lihat sisa last page (kalau=1 --> page-1)
         $sisa = $ada % $disppage;
         echo "<input type='hidden' name='sisa' value='$sisa'>";
         echo "<br><input type='submit' name='btedit' value='EDIT' class='tombol'>";
         echo "<input type='submit' name='btdelete' value='DELETE' class='tombol'>";
         echo "<input type='submit' name='btcancel' value='CANCEL' class='tombol'>";
         echo "</form>";
         echo "</td></tr>";
      } else {
         echo "<tr><td bgcolor='".$tdcol."'>";
         echo "<i>Name</i> : ".$row[nama]." - (";
         if ($row[show_my_email]=="y") { 
            echo $row[email];
         } else {
            echo "<strike>".$row[email]."</strike>";
         }
         echo ")";
         echo " - <i>Postdate</i> : ".date('j F Y - H:i',$row[postdate]);
         echo "<br>".$row[comment];
         echo "<br><a href='".$PHP_SELF."?act=".$row[id]."&start=".$start."#view' class='tdkclpth'>MODIFY</a>";
         echo "</td></tr>";
      }
   }
   echo "</table>";
} else {
   echo "<p><b>No record(s) founds</b></p>";
}

?>

STEP III. Dump this MySQL query :
=================================
CREATE TABLE guestbook (
  id int(11) NOT NULL auto_increment,
  nama varchar(50) default NULL,
  email varchar(50) default NULL,
  show_my_email char(1) default NULL,
  comment text,
  postdate int(11) default NULL,
  PRIMARY KEY  (id),
  KEY id (id)
) TYPE=MyISAM;

STEP IV. CHANGE DATABASE CONFIGURATION AT TOP OF PAGE
=====================================================
ON FILEs "guestbook.php" & "adm_guestbook.php"

$dbhost="localhost";
$dbusername="xxx";
$dbpassword="xxx";
$dbname="xxx";


Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Ridwan Karsadarma

Comment:



Comment:

(Maximum characters: 1200). You have characters left.