Html 页面中有多个表单标签还是一个表单标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12064557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:24:21  来源:igfitidea点击:

multiple form tags in page or one form tag?

htmlxhtml

提问by haansi

I have just started HTML5 and want to know in generalif we should use one <form>tag per web page or multiple form tags.

我刚刚开始使用 HTML5,想知道我们应该<form>每个网页上使用一个标签还是多个表单标签。

Maybe it depends on the situation, but here are two different scenarios:

也许这取决于具体情况,但这里有两种不同的情况:

  1. One Sign Up form
  2. A home page with multiple sub forms: Login, Join Mailing List, etc.
  1. 一份注册表单
  2. 具有多个子表单的主页:登录、加入邮件列表等。

Is it bad to have a form tag inside another or is it ok per standards?

在另一个内部有一个表单标签是不好的,还是按照标准可以?

Thanks

谢谢

EditCan you please explain in a simple way what the purpose of the form tag is?

编辑你能不能简单解释一下表单标签的目的是什么?

回答by Phil

I feel like you've already answered your questions.

我觉得你已经回答了你的问题。

  1. One sign up form = one form tags.
  2. Multiple forms = many form tags.
  1. 一个注册表单 = 一个表单标签。
  2. 多个表单 = 多个表单标签。

Just don't nest them.

只是不要嵌套它们。

EDIT

编辑

Form tagsare meant to hold a variety of fields (i.e. input tags) that you will eventually pass to a target URL using a GET or POST request.

表单标签旨在保存各种字段(即输入标签),您最终将使用 GET 或 POST 请求将这些字段传递给目标 URL。

Take this login form, for example:

以这个登录表单为例:

<form action="login.php">
  <input id="name" type="text" name="name">
  <input id="passwd" type="password" name="passwd">
  <input type="submit" value="Login">
</form>

This has a login, a password, and a submit button. When the "Login" button (type = "submit") is pressed, the form will take that information and send it to another URL (in this case, "login.php", and that URL will handle the information accordingly (e.g. validate, sign you in, display captcha).

它有一个登录名、一个密码和一个提交按钮。当按下“登录”按钮(type =“submit”)时,表单将获取该信息并将其发送到另一个 URL(在本例中为“login.php”,该 URL 将相应地处理信息(例如验证,登录,显示验证码)。

回答by Chiubaka

There is no reason why you can't have multiple forms on a single page. You just can't nest forms, because then the forms aren't able to identify which fields are for what.

没有理由在一个页面上不能有多个表单。您不能嵌套表单,因为这样表单就无法识别哪些字段用于什么。

Conceptually, if you need to have the information for two forms occupy the same section or area on your site (for example, if you were combining your sign-up and email list forms or something), you would use a single form and sort out the information from the POST variable on the other end. So long as you name things in a way that makes sense, you shouldn't even want nested forms to accomplish this.

从概念上讲,如果您需要让两个表单的信息占据您网站上的相同部分或区域(例如,如果您将注册表单和电子邮件列表表单组合在一起),您将使用一个表单并进行整理来自另一端的 POST 变量的信息。只要您以有意义的方式命名事物,您甚至不应该希望嵌套表单来实现这一点。

Edit:

编辑:

To further answer your question, a form tag, in its most basic use case, is used to submit data to a URL. The URL you choose to submit a form to typically receives that data and processes it in some way before taking action on that data, like storing the data in a database, or creating a new user based on a given username and password.

为了进一步回答您的问题,在最基本的用例中,表单标签用于将数据提交到 URL。您选择提交表单的 URL 通常会接收该数据并在对该数据执行操作之前以某种方式对其进行处理,例如将数据存储在数据库中,或根据给定的用户名和密码创建新用户。

回答by mash

Putting forms inside forms doesn't make sense, how would you differentiate the fields inside each form now? Are they part of the master form? The child form? Both?

把表格放在表格里面是没有意义的,你现在如何区分每个表格里面的字段?它们是主表单的一部分吗?儿童形态?两个都?

Separate forms for each area that you will need to read input from is best practice. If you have a login area, make a form for it. If you also have a comment area on that page, a separate form should handle that event.

您需要从中读取输入的每个区域的单独表单是最佳实践。如果您有登录区域,请为其制作一个表格。如果该页面上还有评论区,则应使用单独的表单处理该事件。

回答by Noman_ibrahim

Yes, we can use multiple tags inside one single HTML page. Sometimes we need to submit two distinct data from a single HTML page. To do so we need to use multiple tags. We use a tag to send our data to a particular servlet or JSP (in case of JAVA). We provide information about the client through the . there is an attribute inside the tag called as action="#". We defined the particular servlet name where the data inside the must go.Thus we provide data from a client (HTML) to a servlet (server). Then the servlet manipulates the provided data like inserting the data into the database. The following code can be a help to understand. Here two tags are used for two different task, and also they will be handled by two different servlets.

是的,我们可以在一个 HTML 页面中使用多个标签。有时我们需要从单个 HTML 页面提交两个不同的数据。为此,我们需要使用多个标签。我们使用标签将数据发送到特定的 servlet 或 JSP(在 JAVA 的情况下)。我们通过 . 标签中有一个名为 action="#" 的属性。我们定义了特定的 servlet 名称,其中的数据必须位于其中。因此,我们将数据从客户端 (HTML) 提供给 servlet(服务器)。然后 servlet 操作提供的数据,就像将数据插入数据库一样。下面的代码可以帮助理解。这里两个标签用于两个不同的任务,并且它们将由两个不同的 servlet 处理。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration | Badhon</title>
<link rel="stylesheet" type="text/css" href="registration.css">
</head>


<body background="images/registration.jpg">



 <div class="title">
  <h1>Registration</h1>
 </div>

 <div class="container">

  <div class="left">
       <div><h1>Choose an image (300*300)</h1></div>
   
   
   /* First Form tag ---------------------*/
      <form name="fileform" method="post" action="uploadImage" enctype="multipart/form-data">
    
    <br> <label for="photo"> Portrait Photo: </label> <input
     type="file" name="photo" size="50" placeholder="Upload Your Image"
     required /><br>
    <br> <input type="submit" value="Save">
   </form>
      
 
 /* End of First Form Tag---------------------*/
  
   
   </div>


  <div class="right">
   <div class="formbox">
    
  /* Second Form tag------------------ */      
        <form action="DonarRegister">
     <p>Name</p>
     <input type="text" name="name" placeholder="Name">
     
     <p>Username</p>
     <input type="text" name="username" placeholder="User_name">
     
     <p>Password</p>
     <input type="Password" name="password" placeholder="..........">
     
     <p>Blood Group</p>
     <input type="text" name="bloodgroup" placeholder="O positive">
     
     <p>Age</p>
     <input type="number" name="age">
     
     <p>Mobile Number</p>
     <input type="text" name="mobilenumber" placeholder="......">
     
     <p>email</p>
     <input type="text" name="email" placeholder="......">
     
     <p>Address</p>
     <input type="text" name="address" placeholder="Village/Thana/District"> 
     <input type="submit" name="" value="Register">
        
       <p> <h5>Have already an account !! Then <a href="login.jsp">just login</a></h5></p>



</form>
/* End of Second form tag ----------------- */ 
 
 
   </div>
  </div>
 </div>
</body>
</html>

database and so on.

数据库等。