Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With JavaScript and AJAX, do I still need the HTML <form> tag and do I still need to submit forms to the server using a "submit" button?

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?

like image 444
Nathan Farrington Avatar asked Mar 25 '11 19:03

Nathan Farrington


People also ask

Do I need to use HTML form?

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.

Can we submit form using AJAX?

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.

Does AJAX use HTML?

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)

How do you submit a form using JavaScript?

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.


2 Answers

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.

like image 57
Cᴏʀʏ Avatar answered Oct 03 '22 08:10

Cᴏʀʏ


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.

like image 35
mrK Avatar answered Oct 03 '22 08:10

mrK