w3resource

HTML5: How to specify the name of a form?

Go To Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Opening tag for declaring the document type -->
<html><!-- Opening tag for the root element of the HTML document -->
<head><!-- Opening tag for the document's header section -->
<meta charset="utf-8"><!-- Metadata tag specifying the character encoding of the document -->
<title>Example of HTML name attribute with form</title><!-- Title of the HTML document -->
</head><!-- Closing tag for the document's header section -->
<body><!-- Opening tag for the document's body section -->
<form name="name_example" <!-- Opening tag for the form element with the name attribute set to "name_example" -->
action="https://www.w3resource.com/html-css-exercise/basic/solution/form-name-answer-demo.php" method="get"><!-- Attributes specifying the action URL and method for the form -->
Name : <input type="text" name="name"/><br/><!-- Input field for entering a name -->
<label>Favorite Sports <!-- Label for the select element -->
</label>
<select name="favourite_sports" ><!-- Select element for choosing favorite sports -->
<option value="Soccer">Soccer</option><!-- Option for soccer -->
<option value="Hockey">Hockey</option><!-- Option for hockey -->
<option value="Tennis">Tennis</option><!-- Option for tennis -->
<option value="Golf">Golf</option><!-- Option for golf -->
</select><br/><!-- Closing tag for the select element -->
<label>Describe yourself in short : <!-- Label for the textarea element -->
</label><br/><!-- Line break -->
<textarea cols="10" rows="10" name="descript"><!-- Textarea element for describing oneself -->
</textarea><br/><!-- Closing tag for the textarea element -->
<button type="submit" <!-- Button element for submitting the form -->
name="submit">Submit</button><!-- Button with the name attribute set to "submit" -->
</form><!-- Closing tag for the form element -->
</body><!-- Closing tag for the document's body section -->
</html><!-- Closing tag for the root element of the HTML document -->

Explanation:

  • This HTML code demonstrates a form with various input elements and the name attribute used for identification.
  • The form has the name attribute set to "name_example".
  • The form's action attribute specifies the URL to which the form data will be submitted upon submission, and the method attribute specifies the HTTP method used, in this case, GET.
  • Inside the form element, there are:
    • An input field for entering a name with the name attribute set to "name".
    • A select element for choosing favorite sports with the name attribute set to "favourite_sports".
    • A textarea element for describing oneself with the name attribute set to "descript".
    • A button for submitting the form with the name attribute set to "submit".

Live Demo :

See the Pen form-name-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go To Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/html-css-exercise/basic/form-name-answer.php