添加留言界面验证必填错误提示之后怎么保存用户填写部分的内容?
在添加留言界面,使用php的if语句(前端required在是用返回按钮的时候不能通过)验证必填是否已经填写,如果部分未填弹出错误提示之后,返回的添加界面之前用户填写的部分也清空了,应该怎么给用户保存下来呢?
需要完善的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <?php header( 'content-type:text/html; charset=utf-8' ); header( 'Cache-control: private, must-revalidate' ); date_default_timezone_get( 'PRC' ); $filename = "msg.txt" ; $msgs =[]; if ( file_exists ( $filename )){ $string = file_get_contents ( $filename ); if ( strlen ( $string )>0){ $msgs =unserialize( $string ); } } if (isset( $_POST [ 'pubMsg' ])){ if ( empty ( $_POST [ 'username' ])){ echo "<script>location.href='add.php';alert('用户名为必填字段!');</script>" ; exit ; } else { $username = $_POST [ 'username' ]; if ( empty ( $_POST [ 'title' ])){ echo "<script>location.href='add.php';alert('标题为必填字段!');</script>" ; exit ; } else { $title = strip_tags ( $_POST [ 'title' ]); if ( empty ( $_POST [ 'content' ])){ echo "<script>location.href='add.php';alert('内容为必填字段!');</script>" ; exit ; } else { $content = strip_tags ( $_POST [ 'content' ]); $time =time(); $data =compact( 'username' , 'title' , 'content' , 'time' ); array_push ( $msgs , $data ); $msgs =serialize( $msgs ); if ( file_put_contents ( $filename , $msgs )){ echo "<script>alert('留言成功!'); location.href='index.php';</script>" ; } else { echo "<script>alert('留言失败!'); location.href='index.php';</script>" ; } } } } } if (isset( $_POST [ 'backAdd' ])){ echo "<script>location.href='index.php';</script>" ; } ?> <!DOCTYPE html> <html> <head> <script type= "text/javascript" src= "http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js" ></script> <script type= "text/javascript" src= "http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui" ></script> <link href= "http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel= "stylesheet" media= "screen" > <script type= "text/javascript" src= "http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js" ></script> </head> <body> <div class = "container-fluid" > <div class = "row-fluid" > <div class = "span12" > <div class = "page-header" > <h1> <a href= "index.php" style= "text-decoration:none;" >IMOOC留言板</a> </h1> </div> <div class = "hero-unit" > <h1> 既然来了,就说点什么吧~ </h1> <hr /> <p> 把你想说的写下来,标注好你的姓名、主题,点击提交发送给我们,让小伙伴们都知道你在想些什么. </p> <p> <a rel= "nofollow" class = "btn btn-primary btn-large" href= "http://www.imooc.com/" >访问慕课网 »</a> </p> </div> <?php if ( is_array ( $msgs )&& count ( $msgs )>0):?> <?php endif ; ?> <form method= "post" > <legend>请留言</legend> <div class = "form-group" > <label>用户名</label> <input type= "text" name= "username" style= "height:30px; width:100%" /> </div> <div class = "form-group" > <label>标题</label> <input type= "text" name= "title" style= "height:30px; width:100%" /> </div> <div class = "form-group" > <label>内容</label> <textarea name= "content" style= "height:100px; width:100%" ></textarea> </div> <hr /> <input type= "submit" class = "btn btn-success btn-lg" name= "pubMsg" value= "提交" /> <input type= "submit" class = "btn btn-lg" name= "backAdd" value= "返回" /> </form> </div> </div> </div> </body> </html> |
118
收起
正在回答
4回答
你好,在弹出提示后在应该加上return false,否则页面会重新刷新内容将消失。
格式如下:
1 | echo "<script>location.href='add.php';alert('用户名为必填字段!'); return false ;</script>" ; |
其他部分的弹出提示后同样也要加上return false,如果解决了您的问题请采纳,祝学习愉快!
imooc_澈
2017-07-10 00:10:48
您好,session是系统全局变量,依赖本地的cookie存储,在以后的课程安排中我们会学习到,
敷衍你的敷衍 说法是正确的,要实现跳转回来以后保持原来填写的数据可以使用session,这里具体实现如下:
一进来添加页面就把POST值全存到SESSION中,要注意先要使用session_start()打开session
给表单的input框设置一个默认value值,这个value值就是从SESSION中取得,具体如下,要注意做好相关值是否存在的判断,存在即赋值,以免程序报错,这里也要使用session_start打开session,并且由于有php代码的插入,表单页面的文件后缀必须是.php哦
如果解决了您的问题,请采纳,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧