doukong1897 2013-11-04 03:49
浏览 21
已采纳

添加到数据库。 刷新时不重复

I have this code:

Episode.php

    <?$feedback = new feedback;
$articles = $feedback->fetch_all();

      if (isset($_POST['name'], $_POST['post'])) {
             $cast = $_GET['id'];
             $name = $_POST['name'];
             $email = $_POST['email'];
             $post = nl2br ($_POST['post']);
             $ipaddress = $_SERVER['REMOTE_ADDR'];

if (empty($name) or empty($post)) {
             $error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO comments (cast, name, email, post, ipaddress) VALUES(?, ?, ?, ?, ?)');
     $query->bindValue(1, $cast);
     $query->bindValue(2, $name);
     $query->bindValue(3, $email);
     $query->bindValue(4, $post);
     $query->bindValue(5, $ipaddress);

     $query->execute();
} }?>
<div align="center">
<strong>Give us your feedback?</strong><br /><br />

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="episode.php?id=<?php echo $data['cast_id']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" /> / <input type="text" name="email" placeholder="Email" /><small style="color:#aa0000;">*</small><br /><br />
<textarea rows="10" cols="50" name="post" placeholder="Comment"></textarea><br /><br />
<input type="submit" onclick="myFunction()" value="Add Comment" />
<br /><br />
<small style="color:#aa0000;">* <b>Email will not be displayed publicly</b></small><br />
</form>

</div>

Include.php

class feedback { public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM comments");
      $query->bindValue(1, $cast);
      $query->execute(); return $query->fetchAll();
              } }

This code updates to the database as it is suppose to. But after submission it reloads the current page as mentioned in the form action.

But when I refresh the page to see the comment being added it asks to re submit. If I hit submit then the comment adds again.

How can I stop this from happening?

Maybe I could hide the comment box and display a thank you message but that would not stop a repeat entry.

Please help. Thank you.

Kev

  • 写回答

2条回答 默认 最新

  • duanjiao6730 2013-11-04 04:07
    关注

    You need to add a redirect in there. So at the bottom of your POST block add

    if(isset($_POST['name'], $_POST['post'])) {
        // Do POST stuff here
    
        header('Location: your/url/here');
        exit;
    }
    

    This sends a 302 redirect to the browser and it does a clean load of the page. Since this is a GET operation, there's no reload issues either.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)