当用户提交表单时,我需要在同一页面上为他输出消息。
当我提交表单页面刷新,处理程序触发(php函数),一些POST数据插入数据库,我设置$this['success'] = 1
然后检查Twig:
{% if success %}
here is many tags with information and a message
{% else %}
here is a form
{% endif %}
但是,如果我按F5重新提交表单,则再次执行插入查询。
用AJAX做这个请求不是一个真正的选择。
有很多方法可以回答这个问题(使用缓存,数据库,cookie,会话)。这很容易使用Session Flashing或在这个例子中我将向您展示如何使用Returning a redirect with flash data.
{{ something }}
对于在提交表单后显示我们的回复非常重要。此请求不使用AJAX。
{{ something }}
{{ form_open({ request: 'onSubmit', class: 'row', autocomplete: 'off' }) }}
{{ form_input('text', 'something', '', { class: 'input padding' }) }}
{{ form_submit('Submit', { class: 'button padding' } ) }}
{{ form_close() }}
function onStart() {
$something = Session::get('something');
$this['something'] = $something;
}
function onSubmit() {
$something = Input::get('something');
return Redirect::refresh()->with('something', $something);
}