php开发留言板教程之使用js... 登录

php开发留言板教程之使用js来判断提交数据合法性

QQ截图20161130145506.png

使用js来判断提交数据正确性

<script>
     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>

在提交页面中加入js判断,可以验证提交表单的合法性,能减少错误的发生。


本章重点

1.使用js对用户标题内容进行判断和检测。

下一节
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link href="css.css" rel="stylesheet" type="text/css"> <title>Title</title> <?php include ("add.php")?> </head> <script> 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> <body> <b> <a href="list.php">浏览留言</a> </b> <hr size=1> <form action="add.php" method="post" name="myform" onsubmit="return CheckPost();"> 用户:<input type="text" size="10" name="user"/><br> 标题:<input type="text" name="title" /><br> 内容:<textarea name="content"></textarea><br> <input type="submit" name="submit" value="发布留言" /> </form> </body> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送