Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?

Tags:

php

When I press the 'refresh' button on my browser, it seems that $_POST variable is preserved across the refresh.

If I want to delete the contents of $_POST what should I do? Using unset for the fields of $_POST did not help.

Help? Thanks!

like image 418
Navneet Avatar asked Dec 01 '11 02:12

Navneet


People also ask

How to stop form submitting data on refresh in PHP?

How can stop form submission on page refresh in PHP? After inserting it to database, call unset() method to clear the data.

How to use_ post in PHP?

The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

What is$ post PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

How to prevent page refresh on form submit in laravel?

$(document). on('submit', 'form', function(e) { e. preventDefault(); // your ajax or whatever put here });


1 Answers

The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.

The simple solution is to redirect to a new (if not the same) page. This pattern is very common in web applications, and is called Post/Redirect/Get. It's typical for all forms to do a POST, then if successful, you should do a redirect.

Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.

like image 64
burntblark Avatar answered Sep 19 '22 22:09

burntblark