留言板前加了表单就失效了。
时间:2010-09-11
来源:互联网
之前留言板运行很正常,但是在留言板的前面加入表单代码(<form></form>)留言板就不能往数据库中添加数据了,这是为什么啊。
具体如下:
留言板文件如下(两个文件:add.php和ajaxadd.php)
第一个文件名为:add.php 问题出在(在body后加入<form></form>则留言板失效):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Ajax GuestBook</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<script type="text/javascript">
<!--
//将用户输入异步提交到服务器
function ajaxSubmit(){
if (theForm.title.value.length<1)
{
alert("请填写标题!");
theForm.title.focus();
return false;
}
if (theForm.title.value.length>21)
{
alert("标题不能大于20个字");
theForm.title.focus();
return false;
}
if (theForm.content.value=="")
{
alert("必须填写内容");
theForm.content.focus();
return false;
}
alert('添加成功,正在审核中...');history.go(-1);
//获取用户输入
var title=document.forms[0].title.value;
var author=document.forms[0].author.value;
var content=document.forms[0].content.value;
//创建XMLHttpRequest对象
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//创建请求结果处理程序
xmlhttp.onreadystatechange=function(){
if (4==xmlhttp.readyState){
if (200==xmlhttp.status){
var date=xmlhttp.responseText;
addToList(date);
}else{
alert("error");
}
}
}
//打开连接,true表示异步提交
xmlhttp.open("post", "ajaxadd.php", true);
//当方法为post时需要如下设置http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送数据
xmlhttp.send("title="+escape(title)+"&author="+escape(author)+"&content="+escape(content));}
//将用户输入显示到页面
function addToList(date){
//获取留言列表div容器
var msg=document.getElementById("");
//创建dl标记及其子标记
var dl=document.createElement("dl");
var dt=document.createElement("dt");
var dd=document.createElement("dd");
var dd2=document.createElement("dd"); //将结点插入到相应的位置
msg.insertBefore(dl,msg.firstChild);
dl.appendChild(dt);
dl.appendChild(dd);
dl.appendChild(dd2);
//填充留言内容
dt.innerHTML="标题:"+document.forms[0].title.value;
dd.innerHTML="作者:"+document.forms[0].author.value+" 日期:"+date;
dd2.innerHTML=document.forms[0].content.value;
//清空用户输入框
document.forms[0].title.value="";
document.forms[0].author.value="";
document.forms[0].content.value="";
}
//-->
</script>
</head>
<body>
<!-- 加入下面两行代码就失效了,把这两行删除又正常了。若加这两行放在留言板的后面,留言板也能正常添加。
<form>
</form>
-->
<div id="postBox">
<form name="theForm" method="post">
<dl>
<dt>发表您的留言</dt>
<dd>标题:<input type="text" maxlength="150" size="45" name="title"/></dd>
<dd>作者:<input type="text" maxlength="50" size="45" name="author"/></dd>
<dd>内容:<textarea rows="10" cols="45" name="content"></textarea></dd>
<dd class="button">
<input type="button" onClick="ajaxSubmit()" value="提交"/>
<input type="reset" value="重填"/>
</dd>
</dl>
</form>
</div>
<div id="msgList">
<?php
error_reporting(0);
$db=mysql_connect('localhost','root','');
$select=mysql_select_db("yxdh");
mysql_query("SET NAMES GBK");
$sql="select title,author,content,date from liuyan order by date desc";
$result=mysql_query($sql);
while($rs=mysql_fetch_array($result))
{
?>
<dl>
<dt>标题:<?php echo $rs['title'].'('.$rs['date'].')';?></dt>
<dd>作者:<?php echo $rs['author'];?> 日期:<?php echo $rs['date'];?></dd>
<dd><?php echo $rs['content'];?></dd>
</dl>
<?php
}
?>
</div>
</body>
</html>
第二个文件:ajaxadd.php
<?php
error_reporting(0);
function unescape($str)//定义unescape解码函数(解决js传递乱码问题)
{
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v)
{
if(substr($v,0,2) == "%u" && strlen($v) == 6)
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
return join("",$ar);
}
$db=mysql_connect('localhost','root','');
$select=mysql_select_db("yxdh");
mysql_query("SET NAMES GBK");
$title=unescape($_POST[title]);//调用unescape函数
$author=unescape($_POST[author]);
$content=unescape($_POST[content]);
$date=date("Y-m-d H:i:s");
$sql="insert into liuyan(title,author,content,date) values('$title','$author','$content','$date')";
echo $date;
$result=mysql_query($sql);
mysql_free_result($result);
?>
数据库结构如下:
数据库名:yxdh 表名:liuyan
'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" title="Click Here To EnLarge">
具体如下:
留言板文件如下(两个文件:add.php和ajaxadd.php)
第一个文件名为:add.php 问题出在(在body后加入<form></form>则留言板失效):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Ajax GuestBook</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<script type="text/javascript">
<!--
//将用户输入异步提交到服务器
function ajaxSubmit(){
if (theForm.title.value.length<1)
{
alert("请填写标题!");
theForm.title.focus();
return false;
}
if (theForm.title.value.length>21)
{
alert("标题不能大于20个字");
theForm.title.focus();
return false;
}
if (theForm.content.value=="")
{
alert("必须填写内容");
theForm.content.focus();
return false;
}
alert('添加成功,正在审核中...');history.go(-1);
//获取用户输入
var title=document.forms[0].title.value;
var author=document.forms[0].author.value;
var content=document.forms[0].content.value;
//创建XMLHttpRequest对象
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//创建请求结果处理程序
xmlhttp.onreadystatechange=function(){
if (4==xmlhttp.readyState){
if (200==xmlhttp.status){
var date=xmlhttp.responseText;
addToList(date);
}else{
alert("error");
}
}
}
//打开连接,true表示异步提交
xmlhttp.open("post", "ajaxadd.php", true);
//当方法为post时需要如下设置http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送数据
xmlhttp.send("title="+escape(title)+"&author="+escape(author)+"&content="+escape(content));}
//将用户输入显示到页面
function addToList(date){
//获取留言列表div容器
var msg=document.getElementById("");
//创建dl标记及其子标记
var dl=document.createElement("dl");
var dt=document.createElement("dt");
var dd=document.createElement("dd");
var dd2=document.createElement("dd"); //将结点插入到相应的位置
msg.insertBefore(dl,msg.firstChild);
dl.appendChild(dt);
dl.appendChild(dd);
dl.appendChild(dd2);
//填充留言内容
dt.innerHTML="标题:"+document.forms[0].title.value;
dd.innerHTML="作者:"+document.forms[0].author.value+" 日期:"+date;
dd2.innerHTML=document.forms[0].content.value;
//清空用户输入框
document.forms[0].title.value="";
document.forms[0].author.value="";
document.forms[0].content.value="";
}
//-->
</script>
</head>
<body>
<!-- 加入下面两行代码就失效了,把这两行删除又正常了。若加这两行放在留言板的后面,留言板也能正常添加。
<form>
</form>
-->
<div id="postBox">
<form name="theForm" method="post">
<dl>
<dt>发表您的留言</dt>
<dd>标题:<input type="text" maxlength="150" size="45" name="title"/></dd>
<dd>作者:<input type="text" maxlength="50" size="45" name="author"/></dd>
<dd>内容:<textarea rows="10" cols="45" name="content"></textarea></dd>
<dd class="button">
<input type="button" onClick="ajaxSubmit()" value="提交"/>
<input type="reset" value="重填"/>
</dd>
</dl>
</form>
</div>
<div id="msgList">
<?php
error_reporting(0);
$db=mysql_connect('localhost','root','');
$select=mysql_select_db("yxdh");
mysql_query("SET NAMES GBK");
$sql="select title,author,content,date from liuyan order by date desc";
$result=mysql_query($sql);
while($rs=mysql_fetch_array($result))
{
?>
<dl>
<dt>标题:<?php echo $rs['title'].'('.$rs['date'].')';?></dt>
<dd>作者:<?php echo $rs['author'];?> 日期:<?php echo $rs['date'];?></dd>
<dd><?php echo $rs['content'];?></dd>
</dl>
<?php
}
?>
</div>
</body>
</html>
第二个文件:ajaxadd.php
<?php
error_reporting(0);
function unescape($str)//定义unescape解码函数(解决js传递乱码问题)
{
$str = rawurldecode($str);
preg_match_all("/(?:%u.{4})|.+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v)
{
if(substr($v,0,2) == "%u" && strlen($v) == 6)
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
}
return join("",$ar);
}
$db=mysql_connect('localhost','root','');
$select=mysql_select_db("yxdh");
mysql_query("SET NAMES GBK");
$title=unescape($_POST[title]);//调用unescape函数
$author=unescape($_POST[author]);
$content=unescape($_POST[content]);
$date=date("Y-m-d H:i:s");
$sql="insert into liuyan(title,author,content,date) values('$title','$author','$content','$date')";
echo $date;
$result=mysql_query($sql);
mysql_free_result($result);
?>
数据库结构如下:
数据库名:yxdh 表名:liuyan
'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" title="Click Here To EnLarge">
作者: z104911 发布时间: 2010-09-11
你将你添加的form给的name不要和你之前的冲突
作者: sje410 发布时间: 2010-09-11
form表单还有一个属性 action,必须要写,提交的页面,如果是提交到当前页面的话,action=""
作者: 莫愁 发布时间: 2010-09-11
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28