处理表单及其操作:一页或两页?(Handling a form and its action: one page or two?)

编程入门 行业动态 更新时间:2024-10-05 21:22:45
处理表单及其操作:一页或两页?(Handling a form and its action: one page or two?)

在进行网络编程时,似乎有两种不同的方式来处理表单及其操作。 有没有理由更喜欢一种方法而不是另一种?

方法1:显示表单或处理提交表单的一个页面。 这看起来很简单,因为每件事都有一种形式。 但这也意味着if / else的情况可能变得相当大。

if [form has not been submitted] { // display form for user input } else { // handle submitted form }

方法2:使用表单在一个页面上处理用户输入,并将表单提交到第二页。

第1页(input.html):

<form action="./submit.html"> // display form for user input </form>

第2页(submit.html):处理表单中的输入。

我已经见过这两种方法。 第一种方法的优点是只有一个页面和一组变量需要担心,但页面可能会变大。 在第二种方法中,每个文件更简单且更短,但是现在您拥有需要担心和维护的文件的两倍。

When doing web programming there seem to be two different ways of handling a form and its action. Is there a reason to prefer one method over the other?

Method 1: One page that displays a form or handles a submitted form. This seems simple, in that there's one form for everything. But it also means the if/else cases can become rather large.

if [form has not been submitted] { // display form for user input } else { // handle submitted form }

Method 2: Handle user input on one page with a form, with the form submitting to a second page.

page 1 (input.html):

<form action="./submit.html"> // display form for user input </form>

page 2 (submit.html): Handles the input from the form.

I've seen both of these methods used. The upside of the first method is that there's only one page and one set of variables to worry about, but the page can get large. In the second method, each file is simpler and shorter, but now you have twice as many files to worry about and maintain.

最满意答案

我通常会提交回同一页面,因为如果存在验证错误,您通常需要重新显示初始页面。

记录成功保存后,我通常使用Post / Redirect / Get方法来避免常见的多重提交问题。

I typically submit back to the same page, as you will often need to redisplay the initial page if there are validation errors.

Once the record has been successfully saved, I typically use the Post/Redirect/Get method to avoid the common multiple submit issue.

更多推荐

本文发布于:2023-08-04 22:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   操作   Handling   page   action

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!