dimanche 24 novembre 2019

How do I resend a post request that has already been processed in php?

I see a lot of questions where people are trying to prevent a POST request from being resent, but I want the opposite. I want to have a retry button that resends the POST data that has already been sent.

In Chrome, when I manually refresh the page the POST request is resend when I choose OK on the warning. However, when I try to replicate this by making a button that refreshes the page it does so without the original POST data.

Here's the php:

<?php

    $name = $_POST["titleChoice"];

    if (sizeof($_POST["choice"]) != 0){
        $randomNumber = rand(1, sizeof($_POST["choice"])); 

        echo "<b>" . $name . "</b><br><br>";

        for ($i = 1; $i <= sizeof($_POST["choice"]); $i++) {
            if ($randomNumber == $i){
                echo  "<b>" . $i . ". " . $_POST["choice"][$i-1] . "</b><br>";
            } 
            else{       
                echo  $i . ". " . $_POST["choice"][$i-1] . "<br>";
            }
        }   
    }
    else{
        echo "No choices were inputted! Would you like to try again?";
    }

?>

And here's the retry button:

<form action="process.php" method="post">
    <button name="submit" class="btn-lg" onclick="reloadPage();"> Retry </button>
</form>

 <script type="text/javascript">
    function reloadPage()
    {
        window.location.reload();
    }
 </script>

I'm new to PHP so any help would be appreciated, thanks!




Aucun commentaire:

Enregistrer un commentaire