萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php 簡單留言板教程三

php 簡單留言板教程三

好了,其它的都快完了我們就來看看index.php文件顯示留方的頁面吧.

<?php
 @session_start();
 include("global.php");
 
 if(isset($_GET['act']) && $_GET['act'] == 'del')
 {
  if(isset($_GET['mid']))
  {
   $mid = intval($_GET['mid']);
   if(mysql_query("DELETE FROM xx_guestbook WHERE mid={$mid}"))
   {
    echo("delete OK!");
    die("<script>window.location.href='{$_SERVER['PHP_SELF']}';</script>");
    //echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php\">";
   }
   else echo("false");
  }
 }
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>求職留言本</title>
<style type="text/css">
<!--
body {
 background: #C7EDDD;
 text-align: center;
 margin: 0px;
 padding: 0px;
 font-size: 12px;
}
a {
 text-decoration: none;
 margin: 0px;
 padding: 0px;
}
.messagebox img {
 margin: 0px;
 padding: 0px;
}
.all {
 margin: 0px auto;
 padding: 0px 0px 10px;
 width: 780px;
 background: #C7EDCC;
 text-align: left;
}
.header {
 margin: 0px;
 padding: 0px;
 height: 90px;
 background: #DDDDDD;
 border-top: 5px solid #660066;
 border-right: 1px solid #660066;
 border-bottom: 1px solid #660066;
 border-left: 1px solid #660066;
}
.header h1 {
 font-size: 36px;
 font-weight: bold;
 margin: 0px;
 padding: 5px 10px 0px;
}
.header #menu {
 margin: 0px;
 padding: 25px 10px 0px;
 height: 20px;
 font-size: 14px;
 font-weight: bold;
 text-align: right;
}
.messagebox {
 margin: 20px 10px 0px;
 padding: 0px 0px 5px;
 width: 760px;
 border: 1px solid #33FFFF;
 background: #C7EDCC;
}
.messagebox a {
 text-decoration: none;
 margin: 0px;
 padding: 0px;
}
.messagebox-top {
 border-bottom: 1px dashed #00FFFF;
 margin: 0px;
 padding: 3px 0px 0px;
 height: 20px;
 background: #CCFFFF;
}
.messagebox-topl {
 margin: 0px 0px 0px 10px;
 padding: 0px;
 float: left;
}
.messagebox-topr {
 margin: 0px 5px 0px 0px;
 padding: 0px;
 float: right;
}
.messagebox-topc {
 margin: 1px;
 padding: 0px 20px;
}
.messagebox-show {
 margin: 5px 10px 0px;
 padding: 3px 3px 5px;
 overflow: hidden;
 border: 1px solid #00FFCC;
}
.messagebox-reply {
 border-top: 1px dashed #00FFCC;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-right-style: dashed;
 border-bottom-style: dashed;
 border-left-style: dashed;
 margin: 2px 10px 0px;
 padding: 2px 2px 5px;
 background: #E0EDDC;
}
.messagebox-replyt {
 font-weight: bold;
 color: #0000FF;
 margin: 0px 0px 5px;
 padding: 0px;
}
.pages {
 background: #00FFFF;
 margin: 5px 20px 0px;
 padding: 0px 0px 5px;
}
.foot {
 background: #E0EDDC;
 margin: 5px 0px 0px;
 padding: 5px;
 text-align: center;
}
-->
</style>

</head>

<body>
<div class="all">
<div class="header">
 <h1>求職留言本</h1>
    <div id="menu"><a href="index.php">首頁</a>    <a href="madd.php">發表留言</a><?php
  if(!$_SESSION[user])
   echo "<a href=\"login.php\">登陸</a>";
  if($_SESSION[user])
   echo "<a href=\"logout.php\">退出</a>";
 ?>
    </div>
    <!-- menu -->
</div><!-- header -->
<?php
 $page = isset($_GET['page'])?intval($_GET['page']):1;
 $num=10;
 $total=mysql_num_rows(mysql_query("select * from xx_guestbook"));
 $pagenum=ceil($total/$num);
 
 if($page>$pagenum || $page<=0 || empty($page))
 {
     //header("location:index.php?page=1"); DEBUG
  $page = 1;
 }
 
  $offset=($page-1)*$num;
 
 $sql = "SELECT * FROM xx_guestbook ORDER BY posttime DESC LIMIT {$offset},{$num}";
 $query = mysql_query($sql);
 while($info = mysql_fetch_array($query))
 {
?> 

  <div class="messagebox">
    <div class="messagebox-top"><span class="messagebox-topl"><strong><a href="#"><a href="#"><?php echo("{$info['username']}"); ?> </a></strong>發表於 <?php echo("{$info['posttime']}"); ?></span> <span class="messagebox-topr"><a href="#"><img src="images/city.gif" alt="IP:<?php echo("{$info['ip']}"); ?>" /></a> <a href="mailto:<?php  echo("{$info['email']}"); ?>"><img src="images/email2.gif" alt="給TA發email:<?php  echo("{$info['email']}"); ?>" /></a> <a href="tencent://message/?uin=<?php  echo("{$info['oicq']}"); ?>&amp;Site=web&amp;Menu=yes"><img src="images/qq.gif" alt="QQ:<?php  echo("{$info['oicq']}"); ?>" /></a> <a href="<?php echo("{$info['weburl']}"); ?>" target="_blank"><img src="images/web.gif" alt="主頁:<?php echo("{$info['weburl']}"); ?>" /></a></span>
    <?php
  if($_SESSION[user])
  {
 ?>
 <span class="messagebox-topc"><strong><a href="mreply.php?act=edit&amp;mid=<?php echo("{$info['mid']}"); ?>">編輯回復</a>&nbsp;&nbsp;&nbsp;&nbsp; <a onclick="var r=confirm(&quot;確認刪除嗎?&quot;)
  if (r==true)
    {
    window.location.href='<?php $_SERVER['PHP_SELF']; ?>?act=del&mid=<?php echo("{$info['mid']}"); ?>';
    }
  else
    {
    window.location.href='<?php $_SERVER['PHP_SELF']; ?>';
    }" href="#">刪除</a></strong></span>
  <?php
        }
        ?>
  </div>
    <!-- messagebox-top -->
    <div class="messagebox-show"><?php echo("{$info['content']}"); ?></div>
    <!-- messagebox-show -->
    <?php
  if(!empty($info['reply']))
  {
  ?>
      <div class="messagebox-reply">
     
        <div class="messagebox-replyt">admin回復於:<?php echo("{$info['replytime']}"); ?></div>
      <?php echo("{$info['reply']}"); ?></div>
        <?php
        }
        ?>
    <!-- messagebox-reply -->
  </div>
  <!-- messagebox -->

<?php
 }
  $prepage = $page - 1;
  $nextpage = $page + 1;
 
?>

 

  <div class="pages">共 <strong><?php echo("$pagenum"); ?></strong> 頁 當前第 <strong><?php echo("$page"); ?></strong> 頁 <a href="index.php?page=1">第一頁</a> <a href="index.php?page=<?php echo("$prepage"); ?>">上一頁</a> <a href="index.php?page=<?php echo("$nextpage"); ?>">下一頁</a> <a href="index.php?page=<?php echo("$pagenum"); ?>">最後一頁</a> </div><!-- pages -->
 
  <div class="foot">
  菜根 <br />
  Copyright &copy; 菜根  xanxin <a href="http://phpvc.com">http://phpvc.com</a>  </div>
  <!-- foot -->
</div>
<!-- all -->
</body>
</html>
顯示了當然會有留言頁面喽,如下.

<?php
 @session_start();
 include("global.php");
 
 //獲得IP地址 來自sablogX 1.6
 if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  $onlineip = getenv('HTTP_CLIENT_IP');
 } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  $onlineip = getenv('HTTP_X_FORWARDED_FOR');
 } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  $onlineip = getenv('REMOTE_ADDR');
 } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  $onlineip = $_SERVER['REMOTE_ADDR'];
 }
 $onlineip = addslashes($onlineip);
 
 if($_POST['add'] == 'add')
 {
  $username = $_POST['username'];
  $username = htmlspecialchars($username);
  $username = trim($username);
  $qq = $_POST['qq'];
  $email = $_POST['email'];
  $email = htmlspecialchars($email);
  $weburl = $_POST['weburl'];
  $weburl = htmlspecialchars($weburl);
  $content = $_POST['content'];
  $ip = $onlineip;//$_SERVER['REMOTE_ADDR'];
  $posttime = date('Y-m-d H:i:s');
  
  $content = htmlspecialchars($content);
  $content = str_replace("","&nbsp;",$content);
  $content = str_replace(" ","&nbsp;",$content);
  $content = str_replace("\n","<br />",$content);
  
  $sql = "INSERT INTO xx_guestbook(username,email,weburl,oicq,content,posttime,ip) VALUES('$username','$email','$weburl','$qq','$content','$posttime','$ip')";
  if(mysql_query($sql))
  {
   echo("post OK!");
   die("<script>window.location.href='lyb.php';</script>");
   //echo "<meta http-equiv=\"refresh\" content=\"1; url=index2.php\">";
  }
  else
  {
   echo"false!";
  }
 }
 else
 {
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>發表留言</title>
<style type="text/css">
<!--
body {
 font-size: 12px;
 background: #FAFCFF;
}
-->
</style>
<script language=JavaScript>
function FrontPage_Form1_Validator(theForm)
{
  if (theForm.username.value == "")
  {
    alert("請填寫昵稱!");
 history.back();
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length<3)
  {
    alert("昵稱至少應為3個字符!");
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length>15)
  {
    alert("昵稱不能大於15個字符!");
    theForm.username.focus();
    return (false);
  }
  if(theForm.email.value!=""){
              var email1 = theForm.email.value;
              var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
              flag = pattern.test(email1);
              if(!flag){
              alert("郵件地址格式不對!");
     theForm.email.focus();
           return false;
     }
  }
 if(theForm.qq.value!="")
 {
       
  for (i=1;i<theForm.qq.value.length;i++){
      ct=theForm.qq.value.charAt(i);
       if (!(ct>='0'&&ct<='9'))
       {
     alert("QQ號碼只允許輸入0-9數字");
     theForm.qq.focus();
       return(false);
       }
  }
  }

  if (theForm.content.value == "")
  {
    alert("留言內容不能空!");
    theForm.content.focus();
    return (false);
  }
  if (theForm.content.value.length<5)
  {
    alert("留言內容最少5個字符!");
    theForm.content.focus();
    return (false);
  }
  if (theForm.weburl.value != "")
  {
   var weburl = theForm.weburl.value;
 var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/;
 var objExp = new RegExp(Expression);
 if(objExp.test(weburl) != true)
 {
  alert("url地址格式不對!");
      theForm.weburl.focus();
        return false;
 }
  }
 
   return (true);
}
</script>
</head>

<body>
<table width="681" border="0" cellspacing="0" cellpadding="0" align="center">
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="message" id="message" onsubmit="return FrontPage_Form1_Validator(this)">
    <tr>
      <td height="25" colspan="2" align="center"><strong>發表留言(帶*為必填)</strong></td>
    </tr>
    <tr>
      <td width="109" height="25" align="right">呢稱:&nbsp;</td>
      <td width="572" height="25"><label>
        <input name="username" type="text" class="input" id="username" value="<?php
   if($_SESSION[user])
       echo "{$_SESSION[user]}";
 ?>" />
        <font color="#000000">*</font>(<font color="#ff6600">*</font>為必填項)</label></td>
    </tr>
   
   
    <tr>
      <td height="25" align="right">QQ:&nbsp;</td>
      <td height="25"><label>
        <input name="qq" type="text" class="input" id="qq" />
      (純數字)</label></td>
    </tr>
    <tr>
      <td height="25" align="right">E-mail:&nbsp;</td>
      <td height="25"><label>
        <input name="email" type="text" class="input" id="email" size="30" />
      (必須含有@)</label></td>
    </tr>
    <tr>
      <td height="25" align="right">個人主頁(blog):&nbsp;</td>
      <td height="25"><label>
        <input name="weburl" type="text" class="input" id="weburl" size="30" />
      (以http://開關)</label></td>
    </tr>
    <tr>
      <td height="25" align="right">內容:&nbsp;</td>
      <td height="25"><label>
        <textarea name="content" cols="70" rows="15" class="input" id="content"></textarea>
        <font color="#000000">*</font></label></td>
    </tr>
    <tr>
      <td height="25">&nbsp;</td>
      <td height="25"><label>
        <input name="submit" type="submit" class="botton" id="submit" value="發表留言" />
        &nbsp;
        <input name="reset" type="reset" class="botton" value="重新填寫" id="reset" />
        <input name="add" type="hidden" id="add" value="add" />
      </label></td>
    </tr>
  </form>
</table>
</body>
</html>
<?php
 }
?>

OK了簡單的留言系統就OK了.

copyright © 萬盛學電腦網 all rights reserved