0

私はこのコードを持っています:

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>

インクルード.php

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

このコードは、想定どおりにデータベースに更新されます。ただし、送信後、フォーム アクションで説明されているように、現在のページがリロードされます。

しかし、ページを更新してコメントが追加されていることを確認すると、再送信するように求められます。送信を押すと、コメントが再び追加されます。

どうすればこれを防ぐことができますか?

コメント ボックスを非表示にして、お礼のメッセージを表示することもできますが、繰り返し入力を止めることはできません。

助けてください。ありがとうございました。

ケブ

4

2 に答える 2

2

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.

于 2013-11-04T04:07:46.850 に答える
0

走った後

$query->execute();

変数の設定を解除できます:

unset($name, $email, $post);
于 2013-11-04T04:05:37.670 に答える