简体   繁体   中英

php form writing to text file - prevent POST redirect re-submission

The code below is a php form that writes to a txt file and outputs the results on the same page.

However when I refresh the page POST data is automatically added again - and I want to prevent this. I understand I should redirect users to another page, but how do I do that if my results are on the same page and I want them to stay on the same page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Score Watch</title>
 </head>
<body>
 <p>
 <form id="form1" name="form1" method="post" action="">
 <label>Date: &nbsp;
<input name="date" class="datepicker" id="date" required type="text"
data-hint="" value="<?php echo date('d/m/Y'); ?>" />
 </label>
 <br>
 <label>Score:
 <input type="text" name="name" id="name" />
 </label>
  <label>
 <input type="submit" name="submit" id="submit" value="Submit" />
 </label>
 </form>
  </p>
 <?php
    $date = $_POST["date"];
    $name = $_POST["name"];
    $posts = file_get_contents("posts.txt");
    $posts = "$date $name<br>" . $posts;
    file_put_contents("posts.txt", $posts);
    echo $posts;
 ?>
</body>
 </html>

I've seen the header('Location') tactic, but I can't use it since i output data on the same page?

ADD THIS TO TOP OF YOU PAGE

<?php 
if(isset($_POST['name'],$_POST['date'])){
    // Text we want to add
    $posts = "{$_POST['date']} {$_POST['name']}<br>";

    // Add data to top of the file
    file_put_contents("posts.txt", $posts . file_get_contents('posts.txt') );

    // Redirect user to same page
    header('Location: ' . $_SERVER['REQUEST_URI']);
}
?>

Change your body a little bit Replace this:

<?php
    $date = $_POST["date"];
    $name = $_POST["name"];
    $posts = file_get_contents("posts.txt");
    $posts = "$date $name<br>" . $posts;
    file_put_contents("posts.txt", $posts);
    echo $posts;
?>

with this:

<?php
    // Print contents of posts.txt
    echo file_get_contents("posts.txt");
?>

You can use the same page, if you don't want to use another page. The data should be transmitted either via GET or SESSION

To prevent cyclic redirection, you should put in condition your redirection.

I would suggest:

At the top of the page

<?php session_start(); ?>

then

<?php
if (isset($_POST['date'])) {
    $date = $_POST["date"];
    $name = $_POST["name"];
    $posts = file_get_contents("posts.txt");
    $posts = "$date $name<br>" . $posts;
    file_put_contents("posts.txt", $posts);
    $_SESSION['posts'] = $posts;
    header("Location: samepage.php?success=1");
}
?>

<?php
if (isset($_GET['success'], $_SESSION['posts'])) {
    echo $_SESSION['posts']
}
?>

The redirection also should be before the HTML, to prevent sent headers issue.

Only the last block can be printed inside the <p> 's because it's actual output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM