助教你不回答 我只好又来提问了 点击提交之后表格不显示 是哪写错了?找不到啊
来源:2-12 留言板功能实现
Joper
2017-07-11 12:27:13
<?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['pubmsg'])){ $username=$_POST['username']; $title=$_POST['title']; $content=$_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='webpage.php';</script>"; }else{ echo "<script>alert('留言失败!');location.href='webpage.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> this is Pan show time </h1> </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 $var['username']; ?> </td> <td> <?php echo $var['title']; ?> </td> <td> <?php echo date("Y-m-d H:i:s",$var['time']);?> </td> <td> <?php echo $val['content'];?> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif;?> <form action="#" method="post"> <fieldset> <legend>留言处</legend> <label>用户名</label> <input type="text" name="username" required> <label>标题</label> <input type="text" name="title" required> <label>内容</label> <textarea name="content" cols="30" rows="5" required></textarea> <hr color="pink"> <button type="submit" name="pubmsg" class="btn">提交</button> </fieldset> </form> </div> </div> </div> </body> </html>
5回答
慕移动4345823
2017-07-14
你看看第9行, if(strlen($string>0)){ 应该改成 if(strlen($string)>0){ strlen()函数算好$string的长度再和0比较
irise
2017-07-11
同学,您好!小慕去确认了教程,老师的添加留言功能是两个页面实现。下面是小慕给您梳理的添加留言的思路:
1 在添加留言页面(add.php)完成添加后,需要跳到留言展示页面(1.php)
2 您需要在留言展示页面(1.php)取出添加的数据,然后才能显示,并不是你这里添加完location到1.php就能直接显示了,必须要在1.php中把数据取出才行,少了这个取数据的步骤,当然就没办法显示啦!
如果解决了您的问题,请采纳!祝学习愉快!
Joper
提问者
2017-07-11
教程成跳出留言成功之后就在当前页面显示出留言的内容
为啥我的不显示
Joper
提问者
2017-07-11
<?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['pubmsg'])){ $username=$_POST['username']; $title=$_POST['title']; $content=$_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='1.php';</script>"; }else{ echo "<script>alert('留言失败!');location.href='1.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> this is Pan show time </h1> </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 $var['username']; ?> </td> <td> <?php echo $var['title']; ?> </td> <td> <?php echo date("Y-m-d H:i:s",$var['time']);?> </td> <td> <?php echo $val['content'];?> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif;?> <form action="#" method="post"> <fieldset> <legend>留言处</legend> <label>用户名</label> <input type="text" name="username" required> <label>标题</label> <input type="text" name="title" required> <label>内容</label> <textarea name="content" cols="30" rows="5" required></textarea> <hr color="pink"> <button type="submit" name="pubmsg" class="btn">提交</button> </fieldset> </form> </div> </div> </div> </body> </html>
irise
2017-07-11
同学,您好!小慕运行了您的代码,就改了截图中的位置,就可以了的。
下面截图是小慕运行您的代码的效果图:
你说的提交之后的表格显示,需要排查 截图中的这个页面了:
如果你在webpage.php中没有用表格把添加的这个数据显示出来的话,当然是显示不了表格的。小慕建议您可以排查一下webpage.php这个页面是否有用表格显示添加的数据,如果还是搞不定的话,建议您把webpage.php这个页面的代码贴出来,小慕再帮您排查哒。
如果解决了您的问题,请采纳!祝学习愉快!
相似问题