Why use a form tag when you're submitting via ajax?

sprugman :

Philosophical question:

Say I've got a web app that requires javascript and a modern browser, so progressive enhancement is not an issue. If my form is being built via javascript, and my data updates are all being done via ajax POSTs & PUTs, is there really any reason to wrap my controls in a form tag? If I'm still going to use the tag, say for semantic or structural reasons, is there any reason to have action and method params that I'm going to ignore? It kind of feels like a hold-over from an earlier era to me.

Nicole :

There is at least one important user-experience feature provided specifically by wrapping inputs inside a form tag:

The enter key will submit the form. In fact, in Mobile Safari, this is how you get the "Go" button to appear on the keyboard.

Without a form wrapping the inputs, there is nothing to submit.

You can of course provide enter-key behavior through a keypress event, but I don't know about if this works for mobile devices. I don't know about you, but I'd rather work with the semantics provided by the browser than have to imitate them with events.

In your case, you would simply provide an onsubmit event handler for the form, which would do your AJAX submit, then return false, canceling the actual submit.

You can simply provide action="" (which means "self"), and method is not required — it defaults to GET.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Submitting a form via AJAX not working

How to add data to a form when submitting via a POST AJAX request?

Form not submitting "Submit" input via Ajax

submitting a form which is being called via ajax

Submitting form via POST with jQuery and Ajax

Submitting form via Ajax - FormData always empty

form submitting twice via ajax POST

Submitting form via ajax in Laravel 5.1

Angular form is undefined when submitting via ngSubmit

AJAX for redirecting to external domain when submitting form

Why does array not update when submitting form

JSF <f:ajax> tag not rendering my form after submitting

submitting forms in vuejs, should I use the form tag?

submitting form with ajax and php

Submitting a form with ajax in Wordpress

Submitting a form with ajax and laravel

German special characters get stuck when submitting via ajax

How to get Ajax to use CSS form validation before submitting

if use ajax call for contact form submitting, header(location:/path) not working

Ajax form not submitting form rails

Get a form's key value pairs when submitting with ajax

Change the color of messages when submitting a form using Ajax

getting 404 not found when submitting form using ajax in laravel

No ajax post request when submitting form through jquery

Is it a good idea to refresh csrf token when submitting form with ajax?

My form is automatically submitting when i make a ajax call

why this form is not submitting?

How to submit single form via AJAX when you have multiple forms from PHP loop

Do you need to use <script> tag in HTML file when loading React via npm in vscode?