How to fill out a form with an html button. Creating HTML forms. Example: full registration form
Forms are designed to send data from the user to the web server. Forms in HTML can consist of text fields and text areas, checkboxes and radio buttons, and drop-down lists. All these are elements of form. Each element serves to convey some meaning to the site.
At its core, an HTML form is a web page on which you see areas for entering your information. After you fill out the form and click submit, the information from the form is packaged and sent to the web server for processing by a server-side script (handler file). After processing, another web page is returned to you as a response. The following figure clearly demonstrates how the form works:
There's nothing difficult about creating HTML forms. The easiest way to get an idea of forms is to parse a little HTML code and then see how it works. IN following example The syntax for creating a simple HTML form is shown:
Example: Simple HTML Form
- Try it yourself "
My first form:
Name:
Surname:
Element
Forms are inserted into web pages using the element . It provides a container for all form content, including elements such as text fields and buttons, as well as any other tags HTML language. However, it cannot contain another element
.
To send the form to the server, use the “Submit” button, the same result will be obtained if you press the “Enter” key within the form. If the "Submit" button is not present on the form, the "Enter" key can be used to submit.
Most element attributes affect the processing of the form, not its design. The most common of which are action And method. Attribute action contains the URL to which the information in the form will be sent for processing by the server. Attribute method is the HTTP method that browsers must use to submit form data.
Element
Almost all form fields are created using the element (from the English input - input). Appearance element change depending on the value of its attribute type:
Here are some attribute values type:
Entering text and password
One of the simplest types of form elements is a text field, designed for entering text on a single line. This type text input is set by default, and therefore it is the one-line field that will be displayed if you forget to specify the attribute type. To add a one-line text input field to a form, you should inside the element register the attribute type with text value:
The password input field is a type of regular text field. It supports the same attributes as a single-line text field. Attribute name sets the name of the password entry field that will be sent to the server along with the password entered by the user. To create a password field, you need to set the password attribute to type(password (English) - password):
An example of creating a form with a password field:
Example: Password field
- Try it yourself "
Your login:
Password:
You can use the attribute in conjunction with this attribute maxlenght, the value of which determines maximum amount characters that can be entered in this string. You can also set the length of the input field using the attribute size. By default, most browsers limit the width of a text field to 20 characters. To control the width of new form elements, instead of an attribute size, we recommend using Cascading Style Sheets (CSS).
Attribute value specifies the value that is displayed by default in the text field when the form is loaded. By entering a default value in the field, you can explain to the user exactly what data and in what format you want the user to enter here. This is like a sample, because it is much more convenient for the user to fill out the form, seeing an example in front of him.
Switches (radio)
Element type radio creates switches that use the logic "OR" principle, allowing you to select only one of several values: if you select one position, then all others become inactive. The basic syntax of a toggle element is:
Attribute name for switches is required and plays an important role in combining several switch elements into a group. To combine switches into a group, you must set same value attribute name And different meaning attribute value. Attribute value sets the value of the selected radio button to be sent to the server. The value of each radio button element must be unique within the group so that the server knows which response option the user chose.
Attribute presence checked(from English - installed) at the switch element indicates which of the proposed options should be selected by default when loading the page, if necessary. This attribute can be set only for one radio button element from the group:
- Try it yourself "
How old are you?
- under 18
- from 18 to 24
- from 25 to 35
- more than 35
The action attribute.
Main for element is an attribute action, which specifies the data handler for the form. A data handler is a file that describes what to do with form data. The result of this processing is a new HTML page that is returned to the browser. In other words, in the attribute action specifies the URL path to a handler file on the server (sometimes called a script page) for processing the form. The syntax is as follows:
The processing file is located on the server mytestserver.com in folder namefolder and the name of the server script that will process the data - obrabotchik.php. All the data you entered into the form on the web page will be transferred to him. The .php extension indicates that the specified form is processed by a script written in PHP. The handler itself can be written in another language, for example it could be the scripting language Python, Ruby, etc.
It is advisable to always set the attribute value action. If the form should pass values to the same page where it is located, provide an empty string as the value of the action attribute: action="".
method attribute
Attribute method specifies how information should be transferred to the server. The form submission method you choose depends on the data you want to submit with the form. The volume of this data plays a major role here. The most popular are two methods of transferring your form's source data from the browser to the server: GET And POST. The method can be set to any one you choose, and if you do not specify it, the default will be used GET. Let's consider the use of each of them.
POST method
Method POST packages form data and sends it to the server without the user noticing, since the data is contained in the body of the message. Web browser, when using the method POST sends a request to the server consisting of special headers followed by form data. Since the contents of this request are only available to the server, the method POST used to transmit confidential data such as passwords, bank card details and other personal information of users. Method POST also suitable for sending large volumes of information, since unlike the method GET, it has no restrictions on the number of transmitted characters.
GET method
As you already know, the main job of a browser is to receive web pages from the server. So when you use the method GET, your browser simply retrieves the web page as it always does. Method GET also wraps form data, but appends it to the end of the URL before sending the request to the server. To understand how the method works GET, let's see it in action. Open the first example from this lesson (Example: Simple HTML Form) in Notepad (for example Notepad++) and make a small change to the HTML code:
Each form must also have a submit button to submit data after filling out the form.
Button structure:
So, to record almost all form elements, the tag is used with the type attribute. To create a button that resets all data from forms, the following structure is used:
To create a text field, there is a text parameter. The following parameters are used: name – field name; size – for the field in symbols; maxlength – the maximum possible number of characters in the field; value – information displayed in the form by default
Example of a form entry with two text fields:
Enter your name:
Enter last name:
The result of the form is shown in the figure.
If you need to enter a large amount of information into a text field, for example, a comment, use the text area form, which is created using a tag
To the above code let's add a text area field:
A comment:
The result of the code working with the text area is shown in the figure.
The next element of the forms are lists that allow you to make a choice from the presented set of values. Tags allow you to create a list form
List entry structure:
In order for the element to be highlighted when the page loads, it is necessary in the tag
A similar selection method is the checkbox and radiobutton form elements. The difference between these elements is that a checkbox allows you to make multiple selections, while a radio button allows only a single selection.
Structure of a checkbox and radio button entry:
text
Radio button:
text
In elements specified in the structure, the checked attribute is used by default to highlight the checkbox and radio button. An example of using a checkbox, radio button, and HTML code is shown in the figure.
Another form element is a button, specified using the type tag attribute with the value button:
In the specified code for creating a button, there is an onclick parameter, which usually specifies code in a programming language to perform a particular action when this button is clicked:
To display a message in a special window, use the JavaScript command – aler. The result of the example is shown in the figure.
To insert an image into a button, use the code shown in the following example:
When registering and logging into websites, a field with a hidden test is used, displayed as stars. This is the password form element:
Registration on the site is often divided into several pages and each subsequent one must contain information from the previous one. In order to hide the transmitted information, the hidden form element is used:
The hidden form element will be invisible in the browser window.
To upload files to the server, forms have a file element. An example code for uploading files to the server is presented below:
So, in this topic we looked at form elements for creating various HTML pages, which, together with script handlers on a computer or server, allow you to develop full-fledged web applications.
26.02.2016
Hi all!
Let's continue learning the basics of HTML. In this lesson we will look at what's happenedHTML form and how it is created.
If you noticed, today not a single website can do without forms. These forms can be different, for example, a feedback form, an order form, a registration and login form, a form for sending comments, reviews, a search form, and many others.
I think now we need to show you examples of how HTML forms look visually.
○ Search form:
○ Site login form:
○ Feedback form:
○ Order form:
I think you have grasped the essence of creating HTML forms. So in today's lesson I will tell you, how to create a simpleHTMLform and what tags can be used to create different form fields.
From now on, I ask for your special attention, because the topic is complex and important.
HTMLform is a field in which the user enters some information. But this information must also be sent somehow. A kind of HTML form is like a motorcycle without gasoline, it seems to have everything, but for some reason it doesn’t move. So, in order for the form to work and send the information entered by the user, you need to create a form processor in the . But don’t bother with the handler, since you are now only taking a course for beginners. We will look at the topic of the form handler in the “PHP” section. So, somehow I got distracted from the main topic.
Learning to create HTML forms
Any HTML form will consist of a main tag
The form must be placed between tags
.For tag
*attributes for the tag
NAME – unique name of the form. It is used when there are several forms on one site.
ACTION – This attribute specifies the path to the form handler. Is required.
METHOD – sending method.
- POST - data is transmitted in hidden form
- GET (default) - data is transmitted in clear text through the browser line.
An example of what a tag looks like
Form elements
○ Taginput
Taginput– this part of the form field element is intended for entering small text and numbers.
For tag
no closing tag needed
.
*attributes for the tag< input >
- name - element name
- size - field size
- required – required field
- autofocus – the mouse pointer will immediately be on the field when loading a web page
- maxlength – with this attribute you can specify the maximum number of characters to enter into the field. By default, without the maxlength attribute, an unlimited number of characters can be entered into the field
- placeholder – a hint for the user that will be displayed directly inside the field form (previously the value attribute was used).
- type - element type
Example of fillings:
Element type type
Text field
○ Text field "text":
Result:
○ Password field:
Result:
○ Email field "email":
Result:
○ Button for selecting a file from the computer “file”:
Result:
○ Phone input field “tel”:
Result:
○ Search field:
Result:
○ Color selection field "color":
Result:
○ Field for entering and selecting numbers “number”:
- min – minimum value
- max – maximum value
- step - step.
Result:
○ Date selection field "date":
Result:
○ Field for selecting date and local time in the format (05/05/2015 00:00):
Result:
○ Display a drop-down calendar.
Field for selecting the year and month in the format (July 2015):
Result:
○ Time selection field "time":
Result:
○ Range slider:
Result:
○ Checkbox:
checked – this attribute specifies which checkbox should be enabled by default
Result:
○ Radio switch:
checked – this attribute specifies which radio switch should be enabled by default
Result:
Buttons
○ Button:
value - label on the button
Result:
○ Button for sending data “submit”:
value - label on the button
Result:
○ Reset button:
value - label on the button
Result:
○ Button with picture:
Result:
○ Hidden field
○ select tag
Tagselect– this is part of a form element intended for entering a drop-down list.
For tag
closing tag is required
.
Tag
To create drop-down list items there is a tag closing tag is required .
*attributes for the tag
- Name is the name of the entire list. Set only for tag
- multiple – for multiple selection, only when selecting you need to hold down the “CTRL” key. Set only for tag
- Value – set for each list item for the tag
- disabled - blocks selection of an element in the drop-down list. Set only for tag
Result:
Not urgent Urgent By courier
Or like this:
Result:
Not urgent Urgent By courier
Now let’s block it from the “Urgent” list with the “disabled” attribute:
Result:
Not urgent Urgent By courier
○ drop-down list by group:
To create a group list, use the tag
Result:
Option Textarea
Label Fieldset Legend
○ for multiple selection:
In the tag
Result:
Option Textarea Label Fieldset Legend
Multiline text field
○ Textarea tag
Tagtextarea– this is part of a form field element, intended for entering large text and numbers.
closing tag is required
.
*attributes for the tag< textarea >
- name – field name
- cols – field width
- rows – field height
- placeholder – a hint for the user that will be displayed directly inside the field form.
Result:
Or like this:
Result:
Enter text
Or like this:
Result:
Design “Frame” (fieldset)
○ Fieldset tag
Fieldset tag– using this tag you can draw a frame around an object.
Closing tag is required.
Additional tags
Result:
That's all I wanted to tell you about the topic "HTML forms". Now let’s summarize and in practice we will try to create a simple form, using the knowledge that you have gleaned from this article.
Here's my form:
Result:
Previous post
Next entry
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 a good knowledge of the corresponding programming language and the capabilities of the Unix operating system.
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 should be specified in 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
Tag
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 for transmission 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 a simple HTML form (example 11). It will not collect any data, but will simply return us to the text of this chapter.