1

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?

ToExeCute
  • 61
  • 1
  • 1
  • 9

6 Answers6

3

You can reset the post as follows;

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

Method 2:-

unset($_POST);
Arshid KV
  • 9,631
  • 3
  • 35
  • 36
3

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
}
Reto
  • 1,305
  • 1
  • 18
  • 32
  • Seriously? That's what the OP was asking? Totally unclear question. However if it's asking about not resubmitting the form, you can also just [Post/Redirect/Get](https://en.wikipedia.org/wiki/Post/Redirect/Get). – Mike Oct 20 '16 at 19:25
2

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.

Daniel
  • 4,816
  • 3
  • 27
  • 31
0

How about simply

unset($_POST);
Dave
  • 5,108
  • 16
  • 30
  • 40
0

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;
spp
  • 1
-1
<script type="text/javascript">    
  if (window.history.replaceState) {
    // reset form on refresh.
    window.history.replaceState(null, null, window.location.href);
  }
</script>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Priyank Kotiyal
  • 241
  • 2
  • 13
  • 2
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Mar 25 '23 at 12:12