PHP how to reset POST

✔ Recommended Answer

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}

Source: stackoverflow.com

Answered By: Reto

Method #2

In PHP, you cannot "reset" the $_POST array as it is a superglobal array that is automatically populated by PHP with the data submitted in a POST request. However, you can unset or clear individual elements of the $_POST array.

To unset a specific element of the $_POST array, you can use the unset() function. For example, to unset the value of the "username" input field, you can use the following code:

bash
unset($_POST['username']);

To clear the entire $_POST array, you can use the unset() function in a loop to unset each element of the array. For example, you can use the following code:

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

Alternatively, you can assign an empty array to the $_POST variable to clear it completely. For example:

php
$_POST = array();

Note that this approach will only clear the $_POST array for the current script execution and will not affect any other scripts or subsequent requests.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755