|Just jQuery The Core UI - Forms|
|Written by Ian Elliot|
|Friday, 17 March 2023|
Page 1 of 4
This is an extract from my book Just jQuery The Core UI.
Available as a Book:
buy from Amazon
buy from Amazon
Forms are a special part of the HTML UI because they provide a way to get data back to the server. You can get data back to the server with Ajax, but Forms are still a good way to work. jQuery helps make it easier and more flexible to use forms.
Back in the days before HTML5, forms really were the only UI HTML had – the rest was just text and layout. Today you can build a UI using HTML5 without using a form of any sort, but they are still a useful way to collect data and get it back to the server.
The <form> Element
Forms are collections of controls like buttons and input controls. In the early days of HTML these controls were intended only to appear as child elements of a form but in HTML5 this isn't the case. As a result it is perfectly possible to create what looks like a form without there being any sign of a <form> tag or parent Form Element.
The main purpose of a <form> tag is to specify what is to happen to the data collected by the controls it contains. It does this using two attributes, action and method. The action attribute specifies the URL where the data will be sent and the method is usually either post or get and specifies how the data is sent to the server.
It is important to understand that the <form> doesn't just send data to the server, it implements a full get or post request which results in a web page being sent back to the client. That is, the URL specified determines the next page that the user sees.
<form action="mypage.html" method="get"> </form>
specifies that an HTTP get is performed and the next page to be loaded is mypage.html.
The next question is what triggers the form to actually get the next page?
The answer is that a submit event has to be triggered on the form element. When the form handles the event it automatically gathers all the data in the form and sends it to the server as part of the request for the next page.
There are a number of ways of triggering a submit event but the most common is to include a submit button within the form. For example:
<form action="mypage.html" method="get"> <button type="submit">Submit</button> </form>
If you click the submit button then the submit event is triggered on the parent form element and it gathers the data and performs a get for mypage.html which will load and replace the page that the form is on. If you put an HTML file called mypage.html in the root of the website you will see it load when you click the button.
The user can also trigger a submit event by entering a return in a text field, but this is browser dependent.
The next question is, what sort of data can the form collect and how does it send it to the server?
There are various input controls that you can use. HTML5 expanded the possible range by quite a lot and if you can assume that the client is HTML5 capable then you can create much better forms. However, the basic controls are good enough for many purposes.
In HTML the main form element is the <input type= "type"> where type is any of:
There are also special input tags:
The key idea is that every input element has a name and a value attribute. These define the name/value pairs that the form collects and sends to the server. The value attribute is used to set the initial value of the input element and it is also used if the form is reset.
Consider this basic form that uses all of the above input elements:
<form action="mypage.html" method="get"> <input type="text" name="username"/><br/> <input type="password" name="password" /><br/> <input type="checkbox" name="maths" value="on" />
It creates the following form:
|Last Updated ( Friday, 17 March 2023 )|