PHP message board development tutorial: using js to determine the legality of submitted data

QQ截图20161130145506.png

Use js to judge the correctness of submitted data

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

Add js judgment to the submission page, It can verify the legality of submitted forms and reduce the occurrence of errors.


Key points of this chapter

1. Use js to judge and detect user title content.

Continuing Learning
||
<!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>
submitReset Code