2-9大作业 又出问题了。
主页有出问题的,这样问题我不知道是哪里的问题。。
是的,在11行
但我不明白错哪里了,请老师帮帮忙。
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 | <?php header( "content-type:text/html;chaser=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 ); } } ?> <!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 = "hero-unit" > <h1> <strong>留言板</strong> </h1> <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> <th> 操作 </th> </tr> </thead> <?php foreach ( $msgs as $key => $val ): ?> <tbody> <tr class = "success" > <td> 1 </td> <td> King </td> <td> 标题! </td> <td> <?php echo date ( 'Y-m-d H:i:s' ,time()) ?> </td> <td> 内容! </td> <td> 编辑|删除 </td> </tr> </tbody> </table> <?php endforeach ; ?> <?php endif ; ?> <hr/> <p> <a class = "btn btn-primary btn-large" href= "add.php" rel= "nofollow" >我要留言 »</a> </p> </div> </div> </div> </body> </html> |
43
收起
正在回答
3回答
您好,当您留言成功应该返回index.php,留言失败时应该返回add.php,不应给空的链接地址。由于您给留言成功和留言失败时的跳转地址都是#,导致成功或失败,都会回到首页上,而此时首页中是没有数组中的信息的,会直接没有显示内容。且小慕这边测试您的代码是在点击发布留言按钮之后没有留言显示,并不存在关闭浏览器再次打开时,数据不存在的情况。祝学习愉快!
羽澜星
2017-07-29 21:45:04
首页 代码:
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 | <?php header( "content-type:text/html;chaser=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 [ 'pubMsg' ])){ //接收用户信息 $username = $_POST [ 'username' ]; $title = strip_tags ( $_POST [ 'title' ]); //过滤HTML标记防止注入 $content = strip_tags ( $_POST [ 'content' ]); $time =time(); //将其组成关联数组 $data =compact( 'username' , 'title' , 'content' , 'time' ); //放到$msgs数组中 array_push ( $msgs , $data ); $msgs =serialize( $msgs ); //写入文件 if ( file_put_contents ( $filename , $msgs )){ //成功:弹出提示且返回当前页面 echo "<script>alert('留言成功');locaton.href='#';</script>" ; } else { //失败:弹出提示且返回当前页面 echo "<script>alert('留言失败');locaton.href='#';</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 = "hero-unit" > <h1> <strong>留言板</strong> </h1> <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> <th> 操作 </th> </tr> </thead> <tbody> <?php $i =1; foreach ( $msgs as $key => $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> <td> 编辑|删除 </td> </tr> <?php endforeach ; ?> </tbody> </table> <?php endif ; ?> <hr/> <p> <a class = "btn btn-primary btn-large" href= "add.php" rel= "nofollow" >我要留言 »</a> </p> </div> </div> </div> </body> </html> |
add.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 | <?php header( "content-type:text/html;chaser=utf-8;" ); date_default_timezone_set( 'PRC' ); ?> <!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 = "hero-unit" > <h1> 请写下你想说的话.... </h1> </div> <form class = "form-horizontal" action= "1.php" method= "post" > <div class = "control-group" > <label class = "control-label" >用户名</label> <div class = "controls" > <input type= "text" name= "username" required /> </div> </div> <div class = "control-group" > <label class = "control-label" >标题</label> <div class = "controls" > <input type= "text" name= "title" required /> </div> </div> <div class = "control-group" > <label class = "control-label" >内容</label> <div class = "controls" > <textarea name= "content" rows= "8" cols= "80" ></textarea> </div> </div> <div class = "control-group" > <div class = "controls" > <input type= "submit" name= "pubMsg" value= "发布留言" class = "btn btn-primary btn-large" /> </div> </div> </form> </div> </div> </div> </body> </html> |
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧