如何在同一页面上提交表单后输出消息?

问题描述 投票:0回答:1

当用户提交表单时,我需要在同一页面上为他输出消息。

当我提交表单页面刷新,处理程序触发(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做这个请求不是一个真正的选择。

laravel octobercms
1个回答
1
投票

有很多方法可以回答这个问题(使用缓存,数据库,cookie,会话)。这很容易使用Session Flashing或在这个例子中我将向您展示如何使用Returning a redirect with flash data.

  1. 启动CMS页面
  2. 放置HTML表单代码:*请注意,{{ 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() }}
  3. PHP代码: function onStart() { $something = Session::get('something'); $this['something'] = $something; } function onSubmit() { $something = Input::get('something'); return Redirect::refresh()->with('something', $something); }
  4. 在提交表单后测试并享受一种创建消息的方法。

Photo of a form for testing Photo of the output message

© www.soinside.com 2019 - 2024. All rights reserved.