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

可紊 提交于 2020-01-05 12:13:32

问题


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?


回答1:


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;
?>



回答2:


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.




回答3:


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 :)



来源:https://stackoverflow.com/questions/11626286/how-to-clear-form-data-so-refresh-doesnt-duplicate-data-without-forwarding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!