Adding to database. No repeat on refresh

Posted by kevstarlive on Stack Overflow See other posts from Stack Overflow or by kevstarlive
Published on 2013-11-04T03:49:53Z Indexed on 2013/11/04 3:53 UTC
Read the original article Hit count: 126

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql