php 简单留言板教程三

?phpinclude_once(class.php);$HawkXML = new Message_XML();$Action =
$_GET[Action];?htmlheadmeta content=en /meta name=GENERATOR
content=Zend Studio /meta content=text/html; charset=utf-8 /style
type=text/cssbody{margin: 0;padding: 0;text-align: center;color:
#666;}div{margin: 0 auto;width: 800px;}p,h1,h2,h3,h4,h5,h6{margin: 0;
padding:0}a{text-decoration: none; color:
#0033FF}a:hover{text-decoration: underline}span{font-weight:
bold;}.top{margin-top: 15px;height: 35px;background:
#00FFFF;line-height: 35px;}.msg{height: 25px;text-align: left;margin:
8px;}.msgT,.msgC{text-align:
left;word-break:break-all;}.msgCMD{margin-top: 10px;text-align:
right;}.msgInfo{margin: 3px 0;border: 1px solid #ccc;}.pageCode{margin:
3px 0;height: 35px;line-height: 35px;background:
#eee;}/styletitlephp+xml留言本/title/headbodydiv
class=topphp+xml留言本/divdiv class=msga
href=?Action=post_messagePageNo=?= $_GET[‘PageNo’] ?[发表留言]/a a
href=?Action=show_messagePageNo=?= $_GET[‘PageNo’] ?[显示留言]/a
[a href=?Action=clear_message清空留言/a]/div!–显示留言–div
clas=showmsg?phpswitch($Action){case show_message: //显示留言$HawkXML –
show_message();break;case post_message: //添加留言提交$HawkXML –
post_message();break;case add_message: //添加留言$HawkXML –
add_message($_POST[‘Subject’],$_POST[‘Content’]);break;case
clear_message:$HawkXML – clear_message();break;case delete_message:
//删除留言$AutoID = $_GET[‘AutoID’];$HawkXML –
delete_message($AutoID);break;case edit_message: //修改留言$AutoID =
$_GET[‘AutoID’];$HawkXML – edit_message($AutoID);break;case
save_message: //修改留言提交$AutoID = $_GET[‘AutoID’];$HawkXML –
save_message($AutoID,$_POST[‘Subject’],$_POST[‘Content’]);break;default:
//默认显示留言$HawkXML – show_message();break;}?/div/body/html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“”>
<html xmlns=””>
<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);
       }
  }
  }

?xml version=1.0
encoding=utf-8?rootrecordautoid265634001218784833/autoidsubject测试留言1/subjectcontent测试留言1测试留言1/content/recordrecordautoid140627001218784843/autoidsubject测试留言2/subjectcontent测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2测试留言2/content/recordrecordautoid078128001218784850/autoidsubject测试留言3/subjectcontent测试留言3测试留言3测试留言3/content/recordrecordautoid765628001218784857/autoidsubject测试留言4/subjectcontent测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4测试留言4/content/record/root

  <div class=”messagebox”>
    <div class=”messagebox-top”><span
class=”messagebox-topl”><strong><a href=”#”><a
href=”#”><?php%20echo(“{$info[‘username’]}”); ?>
</a></strong>发表于 <?php%20echo(“{$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 %20echo(“{$info[’email’]}”); ?>”><img
src=”images/email2.gif” alt=”给TA发email:<?php 
echo(“{$info[’email’]}”); ?>” /></a> <a
href=”tencent://message/?uin=<?php %20echo(“{$info[‘oicq’]}”);
?>&Site=web&Menu=yes”><img src=”images/qq.gif”
alt=”QQ:<?php  echo(“{$info[‘oicq’]}”); ?>” /></a>
<a href=”<?php%20echo(“{$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&mid=<?php%20echo(“{$info[‘mid’]}”);
?>”>编辑回复</a>     <a onclick=”var
r=confirm("确认删除吗?")
  if (r==true)
    {
    window.location.href='<?php $_SERVER[‘PHP_SELF’];
?>?act=del&mid=<?php%20echo(“{$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%20echo(“{$info[‘reply’]}”); ?></div>
        <?php
        }
        ?>
    <!– messagebox-reply –>
  </div>
  <!– messagebox –>

今天没事,公司也要写一个php
解析xml的程序,在网上看看了,觉得这个不错就写出来和各位分享吧,我们先看看看留言页面吧.

<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”>呢称: </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: </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: </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): </td>
      <td height=”25″><label>
        <input name=”weburl” type=”text” class=”input” id=”weburl”
size=”30″ />
      (以;
    </tr>
    <tr>
      <td height=”25″ align=”right”>内容: </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″> </td>
      <td height=”25″><label>
        <input name=”submit” type=”submit” class=”botton” id=”submit”
value=”发表留言” />
         
        <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
 }
?>

最后我们把xml文档格式贴出来吧.

<?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”);
  }
 }
?> 

OK了简单的留言系统就OK了.

好了,其它的都快完了我们就来看看index.php文件显示留方的页面吧.

<?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(“”,” “,$content);
  $content = str_replace(” “,” “,$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
 {
?>

  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>
<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))
 {
?> 

 

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

  <div class=”pages”>共 <strong><?php%20echo(“$pagenum”);
?></strong> 页 当前第 <strong><?php%20echo(“$page”);
?></strong> 页 <a
href=”index.php?page=1″>第一页</a> <a
href=”index.php?page=<?php%20echo(“$prepage”);
?>”>上一页</a> <a href=”index.php?page=<?php
echo(“$nextpage”); ?>”>下一页</a> <a
href=”index.php?page=<?php%20echo(“$pagenum”);
?>”>最后一页</a> </div><!– pages –>
 
  <div class=”foot”>
  菜根 <br />
  Copyright © 菜根  xanxin <a
href=”;  </div>
  <!– foot –>
</div>
<!– all –>
</body>
</html>
显示了当然会有留言页面喽,如下.

</head>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;
<html xmlns=”;
<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>

发表评论

电子邮件地址不会被公开。 必填项已用*标注