Jump to content

Prevent submission of form when page refreshed?


php_guy

Recommended Posts

Hello all,

 

As the title says: For sake of example, let's say I have a form with a couple text boxes and a submit button. When you hit submit, the data from the text boxes is translated into a database record.

 

Howver, refershing the page will this record again and again -- which I do not want.

 

How can I prevent this?

 

Keeping in mind the business logic of my applicatoin allows the same record to be entered twice -- however, it should only happen if the user intentionally visits the form agian, and enters the same data. It should not happen on a page refresh.

 

I assume this is a common problem...? Any thoughts?

 

Thanks!

You can use a header() redirect after a successful database insert. Then there will be nothing in the $_POST array if the page is inadvertently refreshed.

 

if( mysql_affected_rows() > 0 ) {

    header('Location: same_page_or_success_message.php');

}

 

Or, you can check for duplicate records first, and present the user with a confirmation dialog.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.