我在我的网站上有以下代码(使用php和smarty)尝试避免在我点击f5时重新提交表单:
if ($this->bln_added == false) {
if (isset($_POST['submit'])) {
$this->obj_site->obj_smarty->assign('title', $_POST['tas_heading']);
$this->obj_site->obj_smarty->assign('desc', $_POST['tas_description']);
}
} else {
$this->obj_site->obj_smarty->assign('title', '');
$this->obj_site->obj_smarty->assign('desc', '');
unset($_POST);
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,bln_added为false,但一旦成功提交表单,则更改为true.模板中使用smarty变量title和desc来保存表单内容,以防出现用户错误并且需要更改输入的内容.
如果表单成功提交,则设置bln_added = true,因此第二位代码不仅要清除表单字段,还要清空$ _POST.但如果按f5,帖子数据仍然存在.
有任何想法吗?
Tom*_*ght 41
你的方法可以在理论上起作用,但有一种更简单的方法.
成功提交表单后,执行重定向.无论在哪里,但它会清除$ _POST.
header('Location: http://www.example.com/form.php');
Run Code Online (Sandbox Code Playgroud)
在您的情况下,听起来您想要重定向到您已经在的页面.如果要显示确认消息,请在URL中附加$ _GET参数.
希望这可以帮助,
汤姆
处理表单的最佳方法是使用自我提交和重定向.像这样的东西:
if (isset($_POST)) {
// Perform your validation and whatever it is you wanted to do
// Perform your redirect
}
// If we get here they didn't submit the form - display it to them.
Run Code Online (Sandbox Code Playgroud)
function index() {
$this->load->library('validation');
// Your validation rules
if ($this->form_validation->run()) {
// Perform your database changes via your model
redirect('');
return;
}
// The form didn't validate (or the user hasn't submitted)
$this->load->view('yourview');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31608 次 |
| 最近记录: |