1、提交表单Javascript验证
<form action="" method="post" name="myform" οnsubmit="return CheckPost();">
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();
……
}
2、str_replace替换函数
替换的值,被替换的内容);
function htmtocode($content) {
$content = str_replace("/n", "<br>", str_replace(" ", " ", $content));
return $content;
}
3、htmlspecialchars 格式化html
主要功能是将HTML格式化,防止在输出html时被浏览器执行
htmlspecialchars(所要格式化对象,定义单双引号,编码)
4、md5加密函数
md5( 加密对象 )
hearder.php:
add.php:
<html>
<head>
<title>添加页面</title>
<style type="text/css">
body{font-size:12px;text-align:center;}
#capation{font-size:24px;font-famliy:"华文新魏";}
</style>
<script type="text/javascript">
function ClearInputWords(){
document.myForm[0].value="";
document.myForm[1].value="";
document.myForm[2].value="请在此输入您的留言!";
}
function CheckInput(){
var user=document.myForm["user"].value;
var title=document.myForm["title"].value;
var content=document.myForm["content"].value;
if(user==""){
alert("用户不能为空!");
document.myForm[0].focus();
return false;
}
if(title==""||title.length<2){
alert("标题不能为空且长度不能小于2个字符");
document.myForm[1].focus();
return false;
}
if(content==""){
alert("内容不能为空!");
document.myForm[2].focus();
return false;
}
}
</script>
</head>
<body>
<?php
/*
* Created on 2010-1-5
* Author:CHAVUET
* Function:留言板
*/
include("header.php");
?>
<span id="capation">添加留言</span>
<form action="conn.php" method="post" name="myForm" οnsubmit="return CheckInput()">
<table>
<tr>
<td>用户:</td>
<td><input type="text" name="user" size="12"/></td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="title" size="18"/></td>
</tr>
<tr>
<td valign="top">内容:</td>
<td><textarea name="content" cols="24" rows="5">请在此输入您的留言!</textarea></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" name="submit" value="提交留言"/>
<input type="button" value="重置" οnclick="ClearInputWords()"></td>
</tr>
</table>
</form>
</body>
</html>
conn.php:
<?php
/*
* Created on 2010-1-4
* Athor: CHAUVET
* Function:处理页面,对数据库操作
*/
if($_POST['submit']!=null){
$conStr=mysql_connect("localhost","root","") or die("连接数据库失败!!!");
mysql_select_db("newdb",$conStr);
mysql_query("set names 'gb2312'");
$str="insert into Message values('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($str);
unset($str);
mysql_close($conStr);
}
header("location:list.php");
?>
list.php:
<html>
<?php
/*
* Created on 2010-1-4
* Author: CHAUVET
* Function:显示用户留言
*/
include("header.php");
$conStr=mysql_connect("localhost","root","") or die("连接数据库出错!");
mysql_select_db("newdb",$conStr);
mysql_query("set names 'gb2312'");
$result=mysql_query("select * from Message");
mysql_close($conStr);
?>
<body>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef" style="margin-top:40px;">
<? while($row=mysql_fetch_array($result)){?>
<tr style="background-color:#eff3ff">
<td>用户:<?=$row["USER"]?> 标题:<?=$row["TITLE"]?></td>
</tr>
<tr style="background-color:#80FF80">
<td>时间:<?=$row["LASTDATE"]?></td>
</tr>
<tr style="background-color:#ffffff">
<td>内容:<?= ReplaceSomeWords($row["CONTENT"])?></td>
</tr>
<? } ?>
</table>
</body>
</html>
login.php:
<?php
include("conn.php");
if($_GET[out]){
setcookie("cookie", "out");
echo "<script language=/"javascript/">location.href='login.php';</script>";
}
if($_POST[id]=='admin'){
$pw=md5($_POST[pw]);
if($pw=='e1bfd762321e409cee4ac0b6e841963c'){
setcookie("cookie", "ok");
echo "<script language=/"javascript/">location.href='login.php';</script>";
}
}
include("head.php");
if($_COOKIE['cookie']!='ok'){
?>
<SCRIPT language=javascript>
function Checklogin()
{
if (myform.id.value=="")
{
alert("请填写登录名");
myform.id.focus();
return false;
}
if (myform.pw.value=="")
{
alert("密码不能为空");
myform.pw.focus();
return false;
}
}
</SCRIPT>
<form action="" method="post" name="myform" οnsubmit="return Checklogin();">
ID:<input type="text" name="id" /><br>
PW:<input type="password" name="pw" /> <input type="submit" name="submit" value="登陆"/>
</form>
<?
}else{
?>
<a href='?out=login'>退出</a>
<?
}
?>