ToExeCute
ToExeCute

Reputation: 71

PHP how to reset POST

I try to reset the Post's after I send the form. In some threads I read that it could be $_POST = array(); but when I tried it, it doesn't works.

Are there any solutions to reset the POST's?

Upvotes: 2

Views: 21107

Answers (6)

Priyank Kotiyal
Priyank Kotiyal

Reputation: 311

<script type="text/javascript">    
  if (window.history.replaceState) {
    // reset form on refresh.
    window.history.replaceState(null, null, window.location.href);
  }
</script>

Upvotes: -1

spp
spp

Reputation: 1

https://stackoverflow.com/a/45199686/17220395

To reload the page keeping the POST data, use:

window.location.reload();

To reload the page discarding the POST data (perform a GET request), use:

window.location.href = window.location.href;

Upvotes: 0

Arshid KV
Arshid KV

Reputation: 10037

You can reset the post as follows;

 foreach ($_POST as $key => $value) {
    $_POST[$key] = NULL;
 }

Method 2:-

unset($_POST);

Upvotes: 3

Daniel
Daniel

Reputation: 4946

I am currently working on this and came with a working solution.

Just using unset($_POST) is not doing the trick. There is still the popup from to resend the information. The way to reset it is actually to send it to another page. This can be done with the header function.

  header("Location: " . $_SERVER['PHP_SELF']);

You will now be free of the message and your $_POST data.

Upvotes: 2

Reto
Reto

Reputation: 1343

In order to prevent a form from being re-submitted, you could do something like this - it should be pretty self explanatory:

function uniquePost($posted) {
    // take some form values
    $description = $posted['t_betreff'].$posted['t_bereich'].$posted['t_nachricht']; 
    // check if session hash matches current form hash
    if (isset($_SESSION['form_hash']) && $_SESSION['form_hash'] == md5($description) ) {
       // form was re-submitted return false
       return false;
    }
    // set the session value to prevent re-submit
    $_SESSION['form_hash'] = md5($description);
    return true;
}

if (isset($_POST["t_submit"]) && uniquePost($_POST)) {
    $ticket_query   =   $db->prepare("INSERT INTO `ticket` (`Absender`, `Betreff`, `Abteilung`, `Prioritat`, `Erstellt`, `Nachricht`) VALUES (:sender, :betreff, :abteilung, :priority, :datum, :nachricht)");
    $ticket_query->execute(array(
    'sender'    =>  $_SESSION["id"],
    'betreff'   =>  $t_betreff,
    'abteilung' =>  $t_bereich,
    'priority'  =>  $t_priority,
    'datum'     =>  date('d.m.Y'),
    'nachricht' =>  $t_nachricht                                        
    ));
    // no need to reset the post variables
}

Upvotes: 4

Dave
Dave

Reputation: 5190

How about simply

unset($_POST);

Upvotes: 0

Related Questions