multiple form tags in page or one form tag?

2019-02-04 08:02发布

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

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.

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

Thanks

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

标签: html html5 xhtml
4条回答
该账号已被封号
2楼-- · 2019-02-04 08:18

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.

<%@ 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.

查看更多
戒情不戒烟
3楼-- · 2019-02-04 08:26

I feel like you've already answered your questions.

  1. One sign up form = one form tags.
  2. Multiple forms = many form tags.

Just don't nest them.

EDIT

Form tags are 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.

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).

查看更多
来,给爷笑一个
4楼-- · 2019-02-04 08:34

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.

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-02-04 08:41

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.

查看更多
登录 后发表回答