添加留言界面验证必填错误提示之后怎么保存用户填写部分的内容?

添加留言界面验证必填错误提示之后怎么保存用户填写部分的内容?

在添加留言界面,使用php的if语句(前端required在是用返回按钮的时候不能通过)验证必填是否已经填写,如果部分未填弹出错误提示之后,返回的添加界面之前用户填写的部分也清空了,应该怎么给用户保存下来呢?

需要完善的代码如下:

<?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>


正在回答

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

4回答

你好,在弹出提示后在应该加上return false,否则页面会重新刷新内容将消失。

格式如下:

 echo "<script>location.href='add.php';alert('用户名为必填字段!'); return false ;</script>";

其他部分的弹出提示后同样也要加上return false,如果解决了您的问题请采纳,祝学习愉快!

  • Michael_Meng 提问者 #1
    不好意思,今天验证了下,按照你的提示加上之后依然不能保留之前用户填写的数据呢
    2017-07-09 13:00:54
imooc_澈 2017-07-10 00:10:48

您好,session是系统全局变量,依赖本地的cookie存储,在以后的课程安排中我们会学习到,

敷衍你的敷衍 说法是正确的,要实现跳转回来以后保持原来填写的数据可以使用session,这里具体实现如下:

一进来添加页面就把POST值全存到SESSION中,要注意先要使用session_start()打开session

http://img1.sycdn.imooc.com/climg//5962542b0001585807850207.jpg

给表单的input框设置一个默认value值,这个value值就是从SESSION中取得,具体如下,要注意做好相关值是否存在的判断,存在即赋值,以免程序报错,这里也要使用session_start打开session,并且由于有php代码的插入,表单页面的文件后缀必须是.php哦

http://img1.sycdn.imooc.com/climg//5962552e0001838e08010625.jpg

如果解决了您的问题,请采纳,祝学习愉快~

irise 2017-07-10 00:07:58

同学,您好!具体的请看截图,三张截图是一个页面的代码哦!注意图中框出来的地方哦!

http://img1.sycdn.imooc.com/climg//5962543300018d8908240638.jpg

http://img1.sycdn.imooc.com/climg//5962548200013dfa09370653.jpg

http://img1.sycdn.imooc.com/climg//596254bc0001f79109680635.jpg

如果解决了您的问题,请采纳!祝学习愉快!

  • 提问者 Michael_Meng #1
    这样设置之后是不是即便是新点开这个界面也会保留上次提交失败的时候填写的部分信息?
    2017-07-10 16:11:58
  • guly 回复 提问者 Michael_Meng #2
    你好,设置之后即便是新点开这个界面也会保留上次提交失败的时候填写的部分信息,这时候如果保存成功的话我们就不需要上次发布的信息,可以在发布成功时清除session,方法为 if(file_put_contents($filename, $msgs)){ session_destroy(); echo "<script>alert('留言成功!');location.href='index.php';</script>"; }else { echo "<script>alert('留言失败!'); location.href='index.php';</script>"; } 如果解决了您的问题请采纳。祝学习愉快!
    2017-07-10 17:24:06
  • 提问者 Michael_Meng 回复 guly #3
    非常感谢你的快速恢复,提交成功的没有问题了,如果有输入框没填,点击提交之后有错误提示,之后用户点击浏览器的返回(页面点击返回我加上session_destroy();没问题)再点击新建,还是会有之前的信息,这种如何取消呢?不好意思,我是不是太多问题了?
    2017-07-10 17:58:10
敷衍你的敷衍 2017-07-09 17:09:35

建议你:1 一进来添加页面就把POST值全存到SESSION中。

             2 给表单的input框设置一个默认value值,这个value值就是从SESSION中取得

如果解决了你的问题,请采纳!祝学习愉快!

  • 提问者 Michael_Meng #1
    不太明白,是否能详细一点呢?或者我可以在哪里查到资料?谢谢啦~
    2017-07-09 18:41:16
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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