R_User
R_User

Reputation: 11082

How to clear form data so refresh doesn't duplicate data without forwarding?

I created a guestbook, in which users can add entries via an HTML form. After submitting data to the guestbook, some users refresh the page. All browsers I know resubmit the data. afterwards. Is there a way to tell the browser, that the data should not be resubmitted?

I used to have a redirect after sucessfully storig the data to the database. But, now I want to promt some additional information to the user, e.g. "Thanks for your entry entitled '...'.", or "A coy of the entry was sent to your e-Mail-Adress...". I really like to avoid adding all the values to GET-Parameters to be able to use the information after the forwarding.

Is there another way (without a forward!) to prevnt the browser from submitting the data again after the user click on the "refresh" button?

Upvotes: 1

Views: 3939

Answers (3)

TigerTiger
TigerTiger

Reputation: 10806

Is there another way (without a forward!) to prevnt the browser from submitting the data again after the user click on the "refresh" button?

Yes - Ajax - and you can still you show all your success/failure messages and even validation....

read this - http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

Example:

var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;  
//alert (dataString);return false;  
$.ajax({  
  type: "POST",  
  url: "bin/process.php",  
  data: dataString,  
  success: function() {  
    $('#contact_form').html("<div id='message'></div>");  
    $('#message').html("<h2>Contact Form Submitted!</h2>")  
    .append("<p>We will be in touch soon.</p>")  
    .hide()  
    .fadeIn(1500, function() {  
      $('#message').append("<img id='checkmark' src='images/check.png' />");  
    });  
  }  
});  
return false;  

Hope this helps.

Upvotes: 1

OdinX
OdinX

Reputation: 4211

Maybe you could set a session variable with a hash of the data posted and check it each time the page is loaded. If the hash is the same, the data has already been submitted:

<?php
session_start(); //Start the session

$dataHash = md5($_POST['name'].$_POST['comment'].$_POST['whatever']); //Get a hash of the submitted data

if(isset($_SESSION['gbHash']) && $_SESSION['gbHash'] == $dataHash) { //Check if the data has been submitted before
    //Do not save the data/show a warning message
} else {
    //Save the data/show a thank you message
}


$_SESSION['gbHash'] = $dataHash;
?>

Upvotes: 3

saad arshad
saad arshad

Reputation: 259

redirect the user with a custom message attached to the redirect location after POST action performed, for example

header("location: http://www.website.com/thankyou.php?msg=email_sent");

or

header("location: http://www.website.com/thankyou.php?msg=email_not_sent");

or

header("location: http://www.website.com/thankyou.php?success=0");

and then switch GET parameters to show corresponding message type. or user AJAX POSTING :)

Upvotes: 0

Related Questions