我自作聪明写了一个函数请看<!--留言发表模块 -->
<form id="form1" name="form1" action="msg.php" method="post" autocomplete="off">
<table id="post_tb" width="100%" border="0" cellspacing="0" cellpadding="0">
中间的昵称啊qq啊内容啊我就省略了。。
<tr>
<td><input type="submit" name="submit" value=" 发 表 " onClick="return FrontPage_Form1_Validator(form1);"/> </td>
</tr>
</table>
</form>
<?php
function msg_post()
{
$a=$_POST['username'];
$b=$_POST['content'];
$c=$_POST['qq'];
$ip=$_SERVER["REMOTE_ADDR"];
include("include/conn.php");
mysql_query("set names gb2312"); //防止出现乱码
$sql = "insert into msg(id,username,qq,content,ip,posttime)
values(NULL,'$a','$c','$b','$ip','20:50:20')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
}
?>
然后页面就是刷新一下添加一个记录我晕。。
前辈们说说怎么办啊?或者同一页内处理这个是不是有什么好方法?谢谢!
<form id="form1" name="form1" action="msg.php" method="post" autocomplete="off">
<table id="post_tb" width="100%" border="0" cellspacing="0" cellpadding="0">
中间的昵称啊qq啊内容啊我就省略了。。
<tr>
<td><input type="submit" name="submit" value=" 发 表 " onClick="return FrontPage_Form1_Validator(form1);"/> </td>
</tr>
</table>
</form>
<?php
function msg_post()
{
$a=$_POST['username'];
$b=$_POST['content'];
$c=$_POST['qq'];
$ip=$_SERVER["REMOTE_ADDR"];
include("include/conn.php");
mysql_query("set names gb2312"); //防止出现乱码
$sql = "insert into msg(id,username,qq,content,ip,posttime)
values(NULL,'$a','$c','$b','$ip','20:50:20')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
}
?>
然后页面就是刷新一下添加一个记录我晕。。
前辈们说说怎么办啊?或者同一页内处理这个是不是有什么好方法?谢谢!
提交到数据库的东西要过滤掉js注入代码,要做的更好一点,把页面和逻辑分离出来,代码封装好一点,留言板采用ajax分页,扩展性,通用性做好,丢给谁都能用
{
if (theForm.username.value == "")
{
alert("昵称不可为空!");
theForm.username.focus();
return (false);
}
if (theForm.content.value == "")
{
alert("留言内容不可为空!");
theForm.content.focus();
return (false);
}
if (theForm.qq.value == "")
{
alert("qq不可为空!");
theForm.qq.focus();
return (false);
}
return (true);
}过滤的话我用的是这个 点按钮的话是可以过滤的
最好的方式是本页面和插入数据库的页分开吗?我只是想方便。。
分页正在搞,
redirect/*
*跳转函数
*/
function redirect($url,$msg,$second=0,$o=true) {
header("Content-type: text/html; charset=utf-8");
$str='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="refresh" content="'.$second.';URL='.$url.'"></head><body>'.$msg.'</body></html>';
if($o){
echo $str;exit;
}
Return $str;
}我放到了我新版queryphp框架里面了,目前这个版本还没有发布
不过已经svn了一部分代码http://code.google.com/p/queryphp/downloads/list
<?
if 上面什么QQ啊之类的文本值为空,就exit;?>
既然 msg_post 被用来处理表单提交,那么就应该判断是否真是表单提交
在 msg_post 函数体开始处应有
f($_SERVER['REQUEST_METHOD'] != "POST") return;
msg_post();
}
这里$_POST实际是一个数组,你可以使用print_r()打印下看看就知道了。如果在$_POST里检测到有submit这个值,就说明是点了提交按钮,这时候,才需要开始执行向数据库里写数据的操作,你这个都没有一个判断,刷新一下就执行了,当然是一个空的值了。