在进行网络编程时,似乎有两种不同的方式来处理表单及其操作。 有没有理由更喜欢一种方法而不是另一种?
方法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.
更多推荐
发布评论