Good day, fans of web development and those who want to create their own website. Before that, all my publications were devoted to the basic elements of language, methods of creating various objects content, their formatting, structuring, etc. Having mastered the previous topics, you can already create a fairly good website. However, it will be incomplete without today’s topic: “Creating forms in html.”
This section of the language is very important. So take the time to study it. Special attention, otherwise the web resource you created will not be released into production. Thus, after reading the article, you will learn what you need to use forms for, what tags they are used to create, and you will also be able to try specific examples on practice. Let's get started!
What is a form and how does it function?
Form– this is one of the most important objects, which is intended for the exchange of information data between the server and the user.
Simply put, if you want to create an online store with the ability to order products on the website, request registration on a web resource and work with accounts, or provide customers feedback with company managers, then you cannot do without forms.
The form is specified using a special element of the html language .
Note that the code document may contain several tag declarations , however, only one request can be sent to the server to process the data. That is why the information that the user enters into the fields provided for this refers to different forms, should not be dependent. Also, it is not allowed to nest forms one within the other.
For those who are impatient and eager to take a quick look at the code representation, I have attached a simple example of using a panel with a text field for a password with a button:
1
2
3
4
5
6
7
8
9
10
11
12
Example
Example
It may not be very clear now what and how interacts in this small program, but I guarantee that after reading the entire article you will be able to create applications that are much more complex.
Sending data to the server side
In order to send the typed (or selected) information in a dialog box, you must use the standard mechanism - Submit button.
The code for such a method looks like this:
When you run the presented line, a button will appear with the inscription: “Submit”.
Another way to send data to the server side is to press the Enter key within the dialog box.
After confirmation of shipment specified information, it does not immediately arrive on the server. First it is processed by the browser, resulting in the form “name=value”.
The attribute parameter is responsible for the name type tag , and for the value - the data entered by the user. Next, the converted string is passed to the handler, which is most often specified in the attribute action element .
The action parameter itself is not required, and in some cases it is not needed at all. For example, if a website page is written with using php or js, then processing occurs on the current page and links are not needed.
For a better understanding of the whole picture of the functioning of the site, I would like to add that on the server, work with data is already performed using other languages. So, server-side languages considered: Python, php, C-like languages (C#, C, etc.), Java and others.
Now I would like to stop and talk more about the element . If you explain in simple language, That needed to create text fields, radio buttons, various buttons, hidden fields, checkboxes and other objects.
The tag does not have to be paired with , however, if you need to process user records or enter them, for example, into a database, then you cannot do without a container.
The main attributes of this language element hypertext markup are:
Text– creates a text field;
Submit– creates a button for sending data to the server;
Image– is responsible for the button with the picture;
Reset– sets a button to clear the form;
Password– sets a text field specifically for passwords;
Checkbox– responsible for fields with checkboxes;
Radio– responsible for fields with the selection of one element;
Button– creates a button;
Hidden– used for hidden fields;
File– sets the field responsible for sending files.
Methods of transmitting information
There are 2 ways to transfer user data to the server side: Get And Post. These methods perform the same action, but they differ significantly from each other. Therefore, before mentioning any of them, let's get acquainted with their features.
Post
Get
Size of transmitted documents
Limited to the server side.
Maximum – 4 KB.
How sent information is displayed
Available only when viewed through browser extensions or other special software products.
Immediately available to everyone.
Using bookmarks
There is no way to add to bookmarks, since requests are not repeated (all pages link to one address).
Any page with a request can be saved as bookmarks and returned to it later.
Used for forwarding large files(books, images, videos, etc.), messages, comments.
Great for searching for requested values on a web resource or for sending short text messages.
In order to indicate which of the two data transfer methods the browser should use, in the element use the provided parameter method(For example, method="post").
Let's look at the second example. Let's create a form in which you need to enter your personal data (first and last name, date of birth) and create a password. Afterwards we send all this to the server using the method Post.
POST method
Enter your personal information!
For example, to enter a date, there are switches for the number of each parameter (day, month and year), as well as a drop-down panel with the calendar itself for convenience.
Creating a Registration Panel
Basic tags and attributes have been covered. That's why it's time to create a full-fledged registration form using style css markup and checking the entered data. Of course, we won’t be able to see the server work with them, but we will provide the design and important details.
I advise you to save this code programs in a document with the .html extension and utf-8 encoding, and open the latter in a browser window. You will see a registration panel in all its glory with fields for entering your first name, last name, e-mail and repeated password. Notice that when you launch the page, the cursor is immediately positioned in the first text field. This technique is achieved through the attribute autofocus.
Start filling out the fields, leaving one unchanged, and click the “Register” button. As you already noticed, the form will not be submitted, since each element <
input> attribute specified required. It sets the marked fields as mandatory.
One more interesting point is a type indication type="email", which appeared in . When using this type of field, the entered information is automatically checked for correctness. In case of errors, the form is not sent to the server.
So the publication has come to an end. In it I tried to collect maximum amount important and relevant knowledge regarding forms. I hope it was useful to you! I would be very grateful if you join the ranks of my subscribers and tell your friends about the blog.
Bye bye!
Best regards, Roman Chueshov
Read: 333 times
An HTML form is a part of a document that allows the user to enter information of interest, which can subsequently be accepted and processed on the server side. In other words, forms are used to collect information entered by users.
To determine which form element the current label belongs to, you must use the for attribute of the tag
Let's look at an example of use:
Example of using a tag Yes No >
In this example we:
Inside first forms:
Posted two radio buttons ( ) to select one of limited number options. Note again that for radio buttons within the same form the same name must be specified, we indicated different values. For first checked , which specifies that the element should be preselected when the page loads (in in this case radio button with the value yes ). In addition, we have specified global attributes for radio buttons that define unique identificator for the element.
Placed two elements , which define the text labels for our text fields. Note that we used the for attribute to determine which form element the current label belongs to. The value of the for attribute corresponds to the value of the global attribute of the radio button we need.
Inside second forms:
Posted two radio buttons ( ) to select one of a limited number of options. For second For radio buttons, we have specified a checked attribute, which indicates that the element should be pre-selected when the page loads (in this case, a radio button with the value no ). In addition, we specified unique values for the radio buttons within the form and the same names.
Placed two elements , we placed our radio buttons inside them. Unlike the previous method, there is no need to specify global attributes (identifier for the element) for radio buttons, and use the for attribute for text labels to refer to them (make a binding).
In the browser, both options (methods) for using text labels look identical:
Tooltip for input fields
Let's look at an example of use:
Example of using the placeholder attribute
IN in this example we specified for the element with type text (a single-line text field) and type password (password field), a text prompt for the user (the placeholder attribute) that describes the expected value for the input.
The result of our example:
Questions and tasks on the topic
Before you start studying next topic take the practical task:
Using the knowledge you have gained, create the following order form:
A nuance: in fields where selection is expected, it should be possible to select by clicking on the text, and not just on the element itself.
After you complete the exercise, inspect the page code by opening the example in a separate window to make sure you did everything correctly.
Tag (from English form- form) installs a form on a web page.
The form is intended for data exchange between the user and the server. The scope of application of forms is not limited to sending data to the server; using client scripts, you can access any element of the form, change it and apply it at your discretion.
A document can contain any number of forms, but only one form can be submitted to the server at a time. For this reason, form data must be independent of each other.
To submit the form to the server, use the Submit button, the same can be achieved by pressing the Enter key within the form. If the Submit button is missing on the form, Enter key imitates its use.
When the form is submitted to the server, control of the data is transferred to the program specified by the attribute action element
The closing tag is required.
WAI ARIA
Default role value: form
Valid role values:
none
presentation
search
Attributes
accept-charset - Sets the encoding in which the server can accept and process data.
action - The address of the program or document that processes the form data.
autocomplete - Enables autocomplete of form fields.
enctype - The way the form data is encoded.
method - HTTP protocol method.
name - The name of the form.
novalidate - Cancels the built-in validation of form data for correctness of input.
target - The name of the window or frame where the handler will load the returned result.
accept-charset
Sets the encoding in which the server can accept and process form data.
Syntax
Values
Encoding name, for example Windows-1251, UTF-8, etc.
Default value
The encoding set for the page.
action
Specifies the handler that form data is accessed when it is submitted to the server. Can act as a handler server program or an HTML document that includes server-side scripts (eg Parser). After the handler performs actions on the form data, it returns a new HTML document.
The value is taken as full or relative path to the server file.
Default value
autocomplete
Controls auto-filling of form fields. The value can be overridden by the autocomplete attribute on specific form elements.
Autofill is performed by the browser, which remembers the values written the first time you enter it, and then substitutes them when you type it again into the form fields. In this case, autocomplete can be disabled in the browser settings and cannot be changed in this case using the autocomplete attribute.
When you enter the first letters of the text, a list of previously saved values is displayed, from which you can select what you need.
Syntax
Values
on - Enables form autofill.
off - Disables autofill. This value is usually used to prevent the browser from saving important data (passwords, numbers bank cards), as well as rarely entered or unique data (captcha).
Default value
enctype
Determines how form data is encoded when it is submitted to the server. Usually there is no need to set the enctype attribute; the data is understood quite correctly on the server side. However, if you are using a file submission field (input type="file"), you should define the enctype attribute as multipart/form-data .
Syntax
Values
application/x-www-form-urlencoded - Instead of spaces, put + , symbols like Russian letters are encoded by them hexadecimal values(for example, %D0%90%D0%BD%D1%8F instead of Anya).
multipart/form-data - Data is not encoded. This value is used when sending files.
text/plain - Spaces are replaced with a + sign, letters and other characters are not encoded.
Default value
application/x-www-form-urlencoded
method
The method attribute tells the server about the request method.
Syntax
Values
The value of the method attribute is case-insensitive. There are two methods - get and post.
get - This method is one of the most common and is designed to obtain the required information and transfer data to address bar. Name=value pairs are then appended to the address after the question mark and separated by an ampersand (& symbol). The convenience of using the get method is that the address with all the parameters can be used repeatedly, saving it, for example, in your browser bookmarks, and you can also change the parameter values directly in the address bar.
post - The post method sends data to the server in a browser request. This allows you to send large quantity data than is available to the get method, since post does not have a 4 KB limit. Large volumes of data are used in forums, mail services, filling databases, sending files, etc.
Default value
name
Defines unique name forms. Typically, the name of the form is used to access its elements through scripts.
Syntax
Values
The name is a set of characters, including numbers and letters. JavaScript is case sensitive, so when accessing a form by name through scripts, use the same spelling as the name attribute.
Default value
novalidate
Cancels the built-in verification of data entered by the user in the form for correctness. This check is carried out automatically by the browser when the form is sent to the server and occurs for fields , , as well as if there is a pattern or required attribute.
Syntax
Values
Default value
By default this attribute is disabled.
target
After the form handler receives the data, it returns the result as an HTML document. You can define the frame into which the resulting web page will load. For this it is used target attribute, its value is the name of the frame. If target is not set, the returned result is shown in the current tab.
Syntax
Values
The value is the name of the frame specified by the element's name attribute
_parent - Loads the page into the parent frame; if there are no frames, then this value works like _self .
_top - Cancels all frames and loads the page in the browser window; if there are no frames, then this value works like _self .
Default value
Specifications
Examples
FORM
Forms can be found on almost every website on the Internet. For example, when you enter your login and password on a website, the data is filled out through forms and sent to the server. Also an example of a form are various surveys.
Tag Syntax
Tag
These will be the buttons: Button one Button two Button three And this will be a text field. For example, you can enter your login here
And this will be a large text field. For example, you can enter information about yourself here
After all of the above there will be an OK button
After clicking OK, the page will simply refresh, because... we did not specify the action parameter
Explanations for example
action="" - indicates that data processing will take place on the same page.
- the type="radio" attribute indicates that you need to display the text after this code as a selection button. The name and value attribute in this tag now play a small role for us, because We are not studying php now (see php lessons).
- the type="text" attribute indicates that this will be a text field. There are also two here important attribute: name (for php) and value (default value).
- the type="textarea" attribute indicates that this will be a large text field. The only difference from the previous case is that it allows you to record a large amount of text.
- the type="submit" attribute indicates that this is a button. The value attribute contains what will be written on the button.
You can read more about all these elements in lesson 15: tag elements
, where radio buttons, lists, checkboxes, text fields, buttons.
Now let's take a closer look at all the tag attributes
.
Tag Attributes and Properties
1. Attribute accept-charset="Encoding"- defines the encoding in which the server can accept and process form data. Can take various values, for example, CP1251, UTF-8, etc.
2. The action="URL" attribute is the address of the script that processes the transmitted data from the form. If you leave this value empty, the data will be processed in the same document where the form is located.
3. Attribute autocomplete="on/off" - sets or disables autocomplete of the form. Can take two values:
on - enable autofill;
off - disable autofill;
4. Attribute enctype="parameter" - specifies the data encoding method. Can take the following values:
application/x-www-form-urlencoded- spaces are replaced with +, characters like Russian letters are encoded with their hexadecimal values
multipart/form-data - data is not encoded
text/plain - spaces are replaced with a + sign, letters and other characters are not encoded.
5. Attribute method="POST/GET" - specifies the sending method. Can take two values:
GET - data transmission in the address bar (there is a limitation on the volume of data sent)
POST - sends data to the server in a browser request (can send a large number of data, because no volume limitation)
6. Attribute name="name" - sets the name of the form. Most often used when there are multiple forms so that you can access a specific form through a script.
7. The novalidate attribute - cancels the built-in check of form data for correctness of input.
8. The target="parameter" attribute is the name of the window or frame where the handler will load the returned result. Can take the following values:
_blank - loads the page into a new browser window
_self - loads the page into the current window
_parent - loads the page into the parent frame
_top - cancels all frames and loads the page in the full browser window
Dear reader, now you have learned much more about html tag form. Now I advise you to move on to the next lesson.
HTML form is a tool with which an HTML document can send some information to some predetermined point in the outside world, where the information will be processed in some way.
It is quite difficult to talk about forms in a tutorial dedicated to HTML. The reason is very simple: creating an HTML form is much easier than creating the "point in the outside world" to which the HTML form will send information. In most cases, such a “point” is a program written in Perl or C.
Programs that process data submitted by forms are often called CGI scripts. The acronym CGI stands for Common Gateways Interface. Writing CGI scripts in most cases requires good knowledge appropriate programming language and capabilities operating system Unix.
Currently, the PHP/FI language has become somewhat widespread, the instructions of which can be embedded directly into HTML documents (the documents are saved as files with the extension *.pht or *.php).
HTML forms pass information to handler programs in the form of [variable name]=[variable value] pairs. Variable names must be specified with Latin letters. Variable values are treated as strings by handlers, even if they contain only numbers.
How the HTML form works
The form is opened with the tag
and ends with the tag
. An HTML document can contain several forms, but the forms should not be located one inside the other. HTML text, including tags, can be placed inside forms without restrictions.
Tag
can contain three attributes, one of which is required. These are the attributes:
Required attribute. Determines where the form handler is located.
Determines how (in other words, using which hypertext transfer protocol method) data from the form will be transferred to the handler. Valid values are METHOD=POST and METHOD=GET . If the attribute value is not set, METHOD=GET is assumed by default.
Determines how data from the HTML form will be encoded to be sent to the handler. If the attribute value is not set, the default is ENCTYPE=application/x-www-form-urlencoded .
The simplest HTML form
In order to start the process of transferring data from the form to the handler, some kind of control is needed. Creating such a control body is very simple:
Having encountered such a line inside the form, the browser will draw a button on the screen with the inscription Submit (read “submit” with an emphasis on the second syllable, from the English “submit”), when clicked, all data available in the form will be transferred to the handler defined in the tag
.
The label on the button can be set to whatever you like by entering the attribute VALUE="[Label]" (читается "вэлью"
с ударением на первом слоге, от английского "значение"),
например:!}
Now we know enough to write simplest HTML form (example 11). It will not collect any data, but will simply return us to the text of this chapter.