如何使用post/redirect/get保持POST方法的输入值? [英] How to keep POST method's input values with post/redirect/get?

查看:97
本文介绍了如何使用post/redirect/get保持POST方法的输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用发布/重定向/获取模式,可以防止重复提交表单.例如,您可以阻止您的在线购物网站的访客两次订购同一商品.

Using the post/redirect/get pattern, you can prevent duplicate form submission. For example, you can prevent a visitor of your online shopping website from ordering the same item twice.

但是,是否可以在访问者刷新页面后不使用AJAX调用的情况下阻止此类重复操作,但在DOM上接收并维护通过POST发送的输入值?

However, is it possible to block such a duplicate action but receive and maintain input values, which are send with POST, on DOM after the visitor refreshes the page without using AJAX call?

为阐明这一点,下面是一个示例.

To clarify the point, here is an example.

  1. 您只有一页,其中有一个<form>,几个<input>和提交"按钮.
  2. 您需要使用POST方法,以便在单击按钮后将运行特定功能(e.g. ordering an item).您需要防止刷新时执行此功能.
  3. 您将使用输入值返回到同一页面,该值在DOM中处于某个位置.
  4. 刷新按钮时,不得运行"2"中描述的功能.但是,即使刷新后也必须保留输入值.
  1. You have only one page, which has a <form>, several <input> and submit button.
  2. You need to use POST method, so that a specific function (e.g. ordering an item) will be run after the button was clicked. You need to prevent this function from getting executed when refreshing.
  3. You'll go back to the same page with an input value, having the value somewhere in the DOM.
  4. When you refresh the button, the function described at "2" must not be run. However, you must hold the input value even after the refreshing.

所以,换句话说...

So, in other words...

点击按钮时,您使用 POST . <input>值将保留在DOM中.

You use POST when you clicked the button. The <input> values will stay in the DOM.

第一次进入页面时,您使用 GET .当然没有<input>值.但是,刷新页面时,将保持<input>.

You use GET when you first arrive at the page. Of course there is no <input> value. However, when you refresh the page, you'll maintain the <input>.

这个问题可能是主观的,但是如果您能提供任何见解,我将不胜感激,因为我没有人要求这种帮助.

This question can be subjective, but I'd really appreciate if you would give any insight, because I have nobody to ask for such a help.

(我使用Laravel/PHP)

(I use Laravel/PHP)

推荐答案

您需要通过GET传输这些值.
有两种方法可以做到这一点:

You need to transport those values via GET.
There are two options to do that:

  1. 会话
  2. URL查询参数

如果仅将数据保存在会话中,则可以从那里读取数据并重新填充表单.如果将其放在URL中,则可以从那里读取它.但是很明显,URL会包含很多数据.

If you simply save the data in the session, you can read it from there and re-populate the form. If you put it into the URL, you can read it from there; but obviously the URL will contain a lot of data then.

从会话中读取数据时,该数据对于特定页面/重定向将不是唯一的,但是无论如何,您在同一会话中再次打开该页面将显示相同的数据.通过URL传递数据将使其对于特定请求是唯一的.

Reading it from a session the data will not be unique to the specific page/redirect, but anyway you open that page again within the same session will show the same data. Passing the data via the URL will make it unique to the specific request.

作为中间立场,您可以将数据保存在与特定随机ID关联的会话中,在URL中以该ID重定向(例如example.com/foo.php?i=12345),然后使用特定ID重新填充会话中的数据

As a middle ground, you can save the data in the session tied to a specific random id, redirect with this id in the URL (e.g. example.com/foo.php?i=12345), the re-populate the data from the session with the specific id.

这篇关于如何使用post/redirect/get保持POST方法的输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆