Back when I learned HTML, CGI was king, and the proper way to interact with the server was to put <input>
tags inside of a <form>
tag. When the user pressed the "submit" button, either an HTTP GET or an HTTP POST message was sent to the server and the browser refreshed using the HTML code returned from the CGI program on the server.
Now I've started doing web stuff again and I've been learning JavaScript and jQuery. Since jQuery can intercept events like click
, keypress
, and focusout
, and can read data with $.ajax()
and $.getJSON()
, is there still a need to have a <form>
tag at all?
Maybe "real forms" are still necessary if I want to support users that have JavaScript turned off?
The HTML form tag is required when you want to collect information that visitors provide. For example, you may want to collect specific data from visitors, such as name, email address, and password. The HTML <form> tag is used to create a form.
We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.
AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)
In this article, we have submitted a form using JavaScript by clicking a link. In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked.
Forms can still help you, but the short answer is no, not really.
If you have a group of inputs that are going to pertain to a certain AJAX request, then it might be easier to keep them in their own form for validation/serialization purposes. For example:
$.ajax({
url: 'ajax/test.html',
dataType: 'json',
method: 'POST',
content: $('form#login_form').serialize(), // <-- Serialize elements only in
success: function(data) { // the login_form container.
$('.result').html(data);
}
});
Depending on your requirements, you may need to keep your forms around for nice degradation for users who have Javascript turned off. You're still going to need to submit the forms, so having the form set up and in place is a good idea.
If you're using ajax, as long as you don't have any direct posts or gets, a form tag is not required. However, it is still good html design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With