为什么在add文件输入后index文件没有显示

为什么在add文件输入后index文件没有显示

这是index.php文件

<?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($filename)>0){
       $msgs=unserialize($string);
   }
};
?>
<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" style="width:1080px;margin:0 auto;">
   <div class="row-fluid">
       <div class="span12">
           <div class="page-header">
               <h1>
                   IMOOC留言板-V1.0
               </h1>
           </div>
           <div class="hero-unit">
               <h1>
                   慕课网留言板,欢迎你!
               </h1>
               <p>
                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、
                   问答社区为核心特点。在这里, 你可以找到最好的互联网技术牛人,也可以通过免费的在线
                   公开视频教材学习国内领先的互联网IT技术。
               </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 $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('m/d/Y H:i:s',$val['time']);?>
                   </td>
                   <td>
                       <?php echo $val['content'];?>
                   </td>
                   <td>
                       编辑|删除
                   </td>
               </tr>
               <?php endforeach;?>
               </tbody>
           </table>
           <?php endif;?>
           <form action="add.php" method="post">
               <input type="submit" class="btn btn-lg btn-primary" value="我要留言"></input>
           </form>
           <div class="page-footer">
               慕课商城 @2017 powered by IMooc.inc
           </div>
       </div>
   </div>
</div>
</body>
</html>


下面是add.php文件

<?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($filename)>0){
       $msgs=unserialize($string);
   }
};
//检测用户是否点击了留言按钮
if(isset($_POST['pubMsg'])){
   $username=$_POST['username'];
   $title=strip_tags($_POST['title']);
   $content=strip_tags($_POST['content']);
   $time=time();
   $data=compact('username','title','content');
   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="add.php";</script>';
   };
};

?>
<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" style="width:1080px;margin:0 auto;">
   <div class="row-fluid">
       <div class="span12">
           <div class="page-header">
               <h1>
                   IMOOC留言板-V1.0
               </h1>
           </div>
           <div class="hero-unit">
               <h1>
                   既然来了,就说点什么吧~~~~
               </h1>
               <p>
                   把你想说的都写在下面,标注好你的姓名,主题,点击提交给我们,让小伙伴们都知道你在想什么。
               </p>
           </div>
           <form action="index.php" method="post" >
               <fieldset>
                   <legend>发布</legend>
                   <div style="width:800px;margin:0 auto">
                   <label>用户名</label><input type="text" name="username" required/>
                   <label>标题</label><input type="text" name="title" required style="width:500px;"/>
                   <label>内容</label><textarea name="content" rows="6" required style="width:500px;"></textarea>
                   <hr/>
                   <input type="submit" class="btn btn-primary btn-lg" name="pubMsg" value="发布留言"></input>
                   <input type="submit" class="btn btn-lg" name="seeMsg" value="查看留言"></input>
       </div>
               </fieldset>
           </form>
       </div>
   </div>
</div>
</body>
</html>

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

你好。代码中可以看出添加的逻辑处理是在add.php中,所以form表单中action应该把index.php改成add.php。如果解决您的问题请采纳,祝学习愉快!

  • 慕仙0328516 提问者 #1
    不是点击提交表单就切换到index.php中,然后在index.php中显示信息吗
    2019-05-02 11:00:31
  • 好帮手慕查理 回复 提问者 慕仙0328516 #2
    您好,但需要提前添加留言,当添加留言完成之后才会有信息展示。所以是在add.php中将信息提交到add.php文件中进行添加的操作,完成之后再跳转到index.php文件中。祝学习愉快!
    2019-05-04 10:10:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
PHP小白零基础入门
  • 参与学习           人
  • 提交作业       626    份
  • 解答问题       4928    个

想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师