<?php
include("conn.php");
if($_POST['submit'])
{
$sql="insert into liu(username,title,nirong)  values ('$_POST[username]','$_POST[title]','$_POST[nirong]')";
mysql_query($sql);
echo "提交成功!!!";
}
?>
<script language="javascript" type="text/javascript">
function bb()
{
if(form1.username.value=="")
{
alert("请填写用户名!");
form1.username.focus();
return false;
}
else
{
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>提交表单</title>
<a href="index.php">添加留言</a> | <a href="chakan.php">浏览留言</a> | <a href="login.php">登陆</a>
</head><body><div align="center">
提交表单
<form action="index.php" method="post" name="form1">
<table align="center">
<tr>
<td>用户名:</td>       <td> <input type="text" name="username" /><br /></td>
</tr>
<tr>
<td>标题:</td>      <td><input type="text" name="title" /><br /></td>
</tr> 
<tr>
<td>内容:</td>       <td><textarea name="nirong" rows="10"  cols="70"></textarea><br /></td>
</tr>
<tr><td></td>              <td><input type="submit" name="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

解决方案 »

  1.   

    script放在header里
    form添加onsubmit="return bb()"
    function bb()
    里else最好添加return true;
    还有,form1.username.value
    最好改成
    document.getElementById("username").value
    当然,这样的话,你每个输入都需要添加ID字段了。
      

  2.   

    修改了还是执行不了javascript的内容
      

  3.   

    我把你的代码copy过来能执行啊。。照楼上的改了吗?再仔细检查一下。
      

  4.   

    就是  要把javascript的代码放在header之间啊