我有表格。成功提交后,服务器端将重定向以查看新条目。
Problem:在按下back按钮后,所有输入仍然使用最后一个值填充(它不会自动提交表单或显示“确认表单提交”提示符)。这允许用户再次手动单击" submit“并提交一个重复的条目,这正是我想要避免的。注意,这不是典型的"repost“问题,我们使用的是post/redirect/get模式。
表单中有20+字段,从技术上讲,可能有两个相同的条目是合法的,但我只想在用户按后退按钮时清除这些字段。
以下是我尝试过的:
无缓存报头
header('Cache-Control: must-revalidate, no-store, no-cache, private, max-age=0');
header('Pragma: no-cache');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');无缓存元标记的组合
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />使用javascript提交和重定向
$('form').submit(function(){
    $.post($(this).attr('action'), $(this).serialize(), function(response){
        window.location = response.redirectUrl;
    });
});“刷新”在提交表单后重定向
header('refresh:0; url=view_entry'); Refresh由会话数据触发(在这里有点烦躁.)
// psuedo PHP code for the "add" page
if ($_SESSION['submit']) {
    $_SESSION['submit'] = 0;
    redirect(current_url); // Cross fingers and hope this clears the form
}
if ($formValid and $saveEntry) {
    $_SESSION['submit'] = 1;
    redirect(next_url);
}所有这些都不起作用,在单击back按钮后,字段仍然被填充。最后一个选项实际上触发了“确认表单提交”提示符,这不是我想要的,但至少它更好一些。
我应该指出,这是在谷歌Chrome。如果这有帮助的话,可以使用Codeigniter和jQuery。我只希望在用户点击“后退”按钮后,在一个成功的条目发布后清除表单字段。我不记得以前有过这样的问题,我已经没有想法了。怎么做才是对的?
发布于 2012-12-12 17:39:14
一种快速简便的方法是在响应后和重定向之前单独清除字段。
$('form').submit(function(){
    $.post($(this).attr('action'), $(this).serialize(), function(response){
        // HERE, clear the fields like...
        $("#field_id_1").val('');
        $("#field_id_2").val('');
        // THEN do the redirect
        window.location = response.redirectUrl;
    });
});它可能不是最全面的解决方案,但由于您正在使用ajax调用进行提交,因此没有理由不这样做。
https://stackoverflow.com/questions/13845231
复制相似问题