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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.