Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Form Tag Necessary in AJAX Web Application?

I read some AJAX-Form tutorial like this. The tag form is used in HTML code. However, I believed that it is not necessary. Since we send HTTP request through XmlHttpRequest, the sent data can be anything, not necessary input in form.

So, is there any reason to have form tag in HTML for AJAX application?

like image 495
Morgan Cheng Avatar asked Apr 24 '10 14:04

Morgan Cheng


People also ask

Does Ajax require a form?

With AJAX, there is really no need to use a form - you can do exactly the same thing without navigating away from the current page (unless you then specifically code it to do so).

Is the form tag necessary?

Form tags are still necessary if you want to be able to submit the form without AJAX (which may be useful in the early stages of development).

Is form tag necessary for input?

The consensus seems to be that FORM tags are no longer required when you simply want to render a form input control. If all your input field is doing is providing a UI (user interface) hook for interactions, then you can put the input fields into the HTML without including a FORM tag.

Can I use input without form?

Yes, you can have a valid input without a form.


2 Answers

Apart from progressive enhancement as already discussed (don't make your site require JavaScript until it really has to), a <form> with onsubmit would be necessary to reliably catch an Enter keypress submission.

(Sure, you can try trapping keypresses on separate form fields, but it's fiddly, fragile and will never 100% reproduce the browser's native behaviour over what constitutes a form submission.)

like image 190
bobince Avatar answered Sep 20 '22 15:09

bobince


Sometimes, web apps using ajax to transform their data either use forms as a fallback when the user has no JavaScript enabled (a sometimes expensive but very good thing to do).

Otherwise, if an application builds and sends an AJAX request, there is no compelling reason to use a form except in rare special cases when you actually need a form element. Off the top of my head:

  • when using jQuery's form serialize function
  • when monitoring all fields in a form for changes
  • when there is need to make use of the reset form button (that to my knowledge is available in a proper <form> only).
like image 44
Pekka Avatar answered Sep 19 '22 15:09

Pekka