Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#9540 closed feature (wontfix)

simplify form submissions

Reported by: rasmus@… Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:

Description

This pattern repeats in basically every jQuery application:

var form = $('form#foo');

$.post(
  form.prop('action'), 
  form.serialize(),
  function (content) {
    // ...
  }
);

Why don't we have something that simply uses the information already contained in the form tag? It already specifies the method (GET or POST), the URL (action) and we know we have to serialize the data on the form, so why not simply:

$('form#foo').send( function (content) {
    // ...
} );

I prototyped a function that will do this here:

http://jsfiddle.net/mindplay/ZDvvm/

Change History (5)

comment:1 Changed 8 years ago by ajpiano

Component: unfiledajax

Thanks for your time and interest in helping out the jQuery project. The jQuery Form Plugin has handled this for quite some time, and my general sense is that we're happier leaving functionality like this outside of core and in the plugin of the user's choice. One complication, of course, is that this doesn't even handle forms that have file inputs. The other assumption that "basically every" jQuery application does this is certainly overstated - there are any number of common UX and other patterns that occur with regularity throughout the jQuery-verse whose inclusion is not merited in core. I've built sites where I needed the form plugin, and sites where I didn't. Since "getting this right" is obviously not as simple as posted here, I think we're going to punt on this, but I'll leave it open for other folks to leave their opinion before closing, but I'm firmly -1 on this one. Sorry to rain on your parade!

comment:2 Changed 8 years ago by timmywil

Priority: undecidedlow
Type: enhancementfeature

I agree with ajpiano.

comment:3 Changed 8 years ago by Rick Waldron

Resolution: wontfix
Status: newclosed

ajpiano +1

comment:4 Changed 8 years ago by rasmus@…

No sweat.

You make some interesting points, and it made me ponder and realize that I have a tendency to think that .post() should be enough for what I need - but it always eventually falls short. Because I'm lazy, I always try it first, but it never does what I want.

For one, when was the last time you found yourself needing to perform a post, but not needing to know whether it succeeded? This is so exotic, the only possible use case I can think of is insignificant stuff like news tickers or status updates. Even so, those would probably be GET rather than POST. How could you not need to handle errors? Never happens. Not where I'm from.

So it seems that I should be using $.ajax() - which also results in more semantic code, due to the named arguments. But also more redundancy.

So it occurs to me, what I need is actually a .ajax() function that wraps $.ajax() and completes any missing type/url/data properties, by picking up the values from a form (or form elements) automatically.

Like so:

http://jsfiddle.net/mindplay/ZDvvm/3/

This takes considerably less code, and it's more open-ended and general purpose, since all it does it provide defaults.

For that matter, you can -1 the $.post() and $.get() functions for all I care - they're not useful, they're a tease ;-)

comment:5 Changed 8 years ago by rasmus@…

Note: See TracTickets for help on using tickets.