老师帮忙看一下代码,提交留言显示留言失败,我把msg.txt文件和PHP文件放在同一级
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | <?php header( 'content-type:text/html;charset:utf-8' ); date_default_timezone_set( 'PRC' ); $filename = "msg.txt" ; $msgs = []; //检测文件是否存在 if ( file_exists ( $filename )){ //读取文件中的内容 $string = file_get_contents ( $filename ); if ( strlen ( $string )>0){ $msgs = unserialize( $string ); } } //检测用户是否点击了提交按钮 if (isset( $_POST [ 'sub' ])){ $username = $_POST [ 'username' ]; $title = strip_tags ( $_POST [ 'title' ]); $content = strip_tags ( $_POST [ 'content' ]); $time = time(); //将其组成关联数组 $date = compact( 'username' , 'title' , 'content' , 'time' ); array_push ( $msgs , $date ); $msgs = serialize( $msgs ); if ( file_put_contents ( $filename , $msgs )){ echo "<script>alert('留言成功!');location.href='lyb.php'</script>" ; } else { echo "<script>alert('留言失败!');location.href='lyb.php'</script>" ; } } ?> <!DOCTYPE html> <html> <head> <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>留言板</title> <meta name= "description" content= "Source code generated using layoutit.com" > <meta name= "author" content= "LayoutIt!" > <link href= "css/bootstrap.min.css" rel= "stylesheet" > <link href= "css/style.css" rel= "stylesheet" > <style> .title1{ margin:40px 0px 40px 0px; } </style> </head> <body> <div class = "container-fluid" > <div class = "row" > <div class = "col-md-12" > <h1 class = "title1" > 留言板功能 </h1> <div class = "jumbotron" > <h2> 欢迎留言! </h2> <p> 你可以在下方输入框内填写您的留言,我们会根据你的留言进行优化和调整,希望对你有帮助! </p> </div> <?php if ( is_array ( $msgs )&& count ( $msgs )>0): ?> <table class = "table" > <thead> <tr> <th> 编号 </th> <th> 用户 </th> <th> 标题 </th> <th> 时间 </th> <th> 内容 </th> </tr> </thead> <tbody> <?php $i = 1; foreach ( $msgs as $val ): ?> <tr class = "success" > <td> <?php echo $i ++;?> </td> <td> <?php echo $val [ 'username' ];?> </td> <td> <?php echo $val [ 'title' ];?> </td> <td> <?php echo date ( "Y-m-d-H:i:s" ), $val [ 'time' ]; ?> </td> <td> <?php echo $val [ 'content' ];?> </td> </tr> <?php endforeach ; ?> </tbody> <?php endif ; ?> </table> <form role= "form" method= "POST" > <div class = "form-group" > <label for = "exampleInputEmail1" > 用户名 </label> <input type= "text" class = "form-control" name= "username" id= "exampleInputEmail1" required= "required" placeholder= "请输入用户吗..." > </div> <div class = "form-group" > <label for = "exampleInputPassword1" > 标题 </label> <input type= "text" class = "form-control" name= "title" id= "exampleInputPassword1" required= "required" placeholder= "请输入标题..." > </div> <div class = "form-group" > <label for = "exampleInputPassword1" > 内容 </label> <input type= "text" class = "form-control" name= "content" id= "exampleInputPassword1" required= "required" placeholder= "请输入内容..." > </div> <button type= "submit" class = "btn btn-primary" name= "sub" style= "width: 200px;height:50px;margin-bottom: 100px;" > 提交 </button> </form> </div> </div> </div> <script src= "js/jquery.min.js" ></script> <script src= "js/bootstrap.min.js" ></script> <script src= "js/scripts.js" ></script> </body> </html> |
2
收起
正在回答 回答被采纳积分+1
1回答
PHP小白零基础入门
- 参与学习 人
- 提交作业 626 份
- 解答问题 4928 个
想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧