PHP实现留言版

开发环境:Ubuntu11.10 ide:Netbeans 

文件结构:

conn.php:负责管理数据库链接;

list.php:显示留言页面;

head.php:头部链接;

add.php:添加留言页;

代码:

//conn.php

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$db_host='localhost';
$db_user='root';
$db_pass='1';
$db_name='bbs';
 $conn=@mysql_connect($db_host,$db_user,$db_pass) or die("error");
 mysql_select_db("bbs",$conn);
 mysql_query("set names 'utf8'");
 //修正添加空格与换行
 function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content));
return $content;
}
?>


//list.php

<?php
include ("conn.php")
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
 <?
 $sql="select * from message ";
 $query=  mysql_query($sql);
//循环显示留言
 while($row=  mysql_fetch_array($query))
 {
 ?>
  <tr bgcolor="#eff3ff">
  <td>标题:<?=$row[title]?> 用户:<?=$row[user]?> </td>
  </tr>
  <tr bgColor="#ffffff">
  <td>内容:<?=htmtocode($row[content])?> </td>
  </tr>
  <?
   }
   ?>
</table>


//head.php

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<link href="images/css.css" rel="stylesheet" type="text/css">
<b><a href="add.php">添加留言</a> | <a href="list.php">浏览留言</a>  | <a href="login.php">登陆</a> </b>
<hr size=1>




//add.php

 

<?php


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 include ("conn.php");


 if($_POST['user'])
 {
    $sql="insert into `message` (`id`,`user`,`title`,`content`,`lastdate`)".
         "values 
         ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
    mysql_query($sql);
     echo "发表成功!";
  }
  include("head.php");
?>

利用javascript验证表单

<script language="javascript">
    function CheckPost()
{
if (myform.user.value=="")
{
alert("请填写用户名");
myform.user.focus();
return false;
}
if (myform.title.value.length<5)
{
alert("标题不能少于5个字符");
myform.title.focus();
return false;
}
if (myform.content.value=="")
{
alert("必须要填写留言内容");
myform.content.focus();
return false;
}
}


</script>
<form action="add.php" method="post" name="myform" οnsubmit="return CheckPost();">
    用户:<input type="text" name="user" size="10"/></br>
    标题:<input type="text" name="title"/></br>
    内容:<textarea name="content" cols="60" rows="9"></textarea></br>
    <input type="submit" name="submit" value="发布留言"/>
</form>



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值