Hot Koehls
  • Email
  • Feedburner
  • Linkedin
  • Twitter
  • Home
  • About
  • Archives
  • Contact
  • Software
    • S3imple Backup
    • Twitter Feed Archiver
    • FileTime
    • Flickr API Demo
Search
Home» For techies » Using jQuery Alerts plugin to submit a form

Using jQuery Alerts plugin to submit a form

Posted by Frank - June 2, 2009 - For techies
4

jQuery is awesome. The jQuery Alerts plugin is also awesome. One of the most immediate and obvious uses of the pair is presenting a spiffy confirmation box before submitting a form. For example, I recently implemented this setup on a form that ultimately deletes data. I couldn’t find a detailed example for this setup, leading to much trial and error. Here’s what worked for me.

First the form…

Option 1
Option 2

Take note that we are dealing with a normal form, nothing fancy about the form action, the inputs, etc. The only thing special going on here is that we’ve substituted an actual <input type="submit" ... /> button for a plain button type and attached an onclick event.

Now the JavaScript…

function confirmDelete() {
  jConfirm('Are you sure you want to delete this stuff?', 'Please Confirm', function(result){
    if (result) {
      $("form[name='action_delete']").submit();
    } else {
      return false;
    }
  });
}
$(document).ready(function(){
  // [ ... ]
});

Assuming you understand the syntax for jQuery Alert, the confirmDelete() function is fairly straightforward. It’s everything around the jConfirm() call that gave me a headache.

Go back to the fact that we’re using a regular button and onclick, instead of a submit button and the onsubmit event. Nested jQuery functions, like jConfirm() and its third argument, have always caused me problems with normal JavaScript events, onsubmit in particular. If not done properly, the submit event is not “held up” by the confirmation dialog, input validation, etc. Such was the case here, so the only submit event occurs in the JavaScript after getting validation from the prompt.

Also note the position of confirmDelete(): outside of the jQuery “document ready” block. If you put it inside, it won’t work.

I explicitly identified the form to submit (line 4 in JavaScript) for sake of code clarity. This layout can easily be modified to dynamically process multiple forms.

Finally, note that this setup requires JavaScript to work, i.e. it does not gracefully degrade. In my opinion, the situations where this is actually a problem are limited. Every major browser on the market fully supports JavaScript, and enables it by default. Unless your target audience is 100% mobile (you should build a separate interface for your mobile app anyway) or in a high security sector, assuming its presence is a very safe bet.

jquery

4 comments on “Using jQuery Alerts plugin to submit a form”

  1. Chris says:
    June 3, 2009 at 8:07 pm

    “If not done properly, the submit event is not “held up” by the confirmation dialog, input validation”

    There is another way to do this. The third argument of the jConfirm() function is just a callback. You could have defined the function with an arbitrary name (the signature of which will be specified by jConfirm) and passed the name of that function in instead. By convention however, jQuery-esque scripts always prefer to inline these things (following the Java tradition of anonymous listeners, etc.).

    “Also note the position of confirmDelete(): outside of the jQuery “document ready” block. If you put it inside, it won’t work.”

    Yep – if you defined confirmDelete() inside of document.ready(), then you would only be able to call confirmDelete() from within document.ready() – that’s just the nature of nested functions (which are supported by JavaScript, Python, et. a. – but not PHP as you’re used to).

  2. Frank says:
    June 3, 2009 at 11:08 pm

    Thanks, Chris. You’re right, if I un-nested all the functions, I could get all the proper return events to fire and thus handle this process with a normal submit button and a call to jQuery’s submit(fn).

    However, that code is (a) a bit longer and (b) not as flexible when it comes to adding more jQuery magic to the mix.

    As for the function scope of confirmDelete(), I was just keeping things simple, as most readers simply want a working solution. However technical background is always a good thing. Thanks for covering for my laziness.

  3. Chris says:
    June 3, 2009 at 11:35 pm

    Frank – certainly most folks who find this information most useful are those in search of a turn-key solution to jQuery form submissions – and for that, the text does it’s job well. That said, I have yet another comment…

    “not as flexible when it comes to adding more jQuery magic to the mix.”

    And you’re absolutely right… that is… it’s not as flexible *at this altitude.* A level higher (or lower, you could make the case) on the abstraction ladder, and you’d find many ways to utilize a non-inlined callback for the form submission. But… then the argument becomes an architectural brow-beating, and not one of just getting the job done already.

    At any rate, for the up-and-coming, jQuery-using crowd, you present several core concepts of jQuery form and function while at the same time applying the lessons to a real example – and that’s just cool.

  4. Frank says:
    June 3, 2009 at 11:40 pm

    at this altitude

    Good qualifier, but you hit the nail on the head with “getting the job done already.” Combinations of HTML, CSS, and JavaScript often present several routes to the same solution. My rule of thumb has become “Does it work? In all browsers? Good, moving on.”

Cancel Reply

Categories

  • For entrepreneurs
  • For everyone
  • For techies

Latest Tweets

  • The PA Report - LucasArts' eulogy reminds us of the inhuman cost of game development http://t.co/vVjHkmr9YD
    April 9, 2013 - 2:46 pm
  • Pictures from a developer's life http://t.co/s75fBPTu4v
    April 5, 2013 - 11:44 am
  • Chuck+Norris http://t.co/awBqZ8gncS
    March 7, 2013 - 10:46 pm
  • Google Hacks http://t.co/ZIU2CHcoem
    March 3, 2013 - 1:08 am

Recent Comments

  • Rohitash on Automating SSH or SFTP in scripts
  • kgiFozzkjk on MySQL founder Michael Widenius concerned about sale to Oracle
  • purusjap on Jeff Atwood still wrong about PHP
  • OTHER FUNCTION on Get HTTP status code of cURL call in PHP
  • CounterSpace on Change timezone to GMT in Debian

Recent Posts

  • Display line numbers in WebSVN file detail view
  • It’s dangerous to go alone
  • Create Self-Signed Wildcard SSL Certificate
  • What comes after the yottabyte?
  • Write code like they do in Hollywood
(c) 2012 Frank Koehl. All Rights Reserved.
  • Contact Us
  • Sitemap