views:

18

answers:

1

I'm using jQuery Ajaxify to ajaxify my forms. It has all the same callbacks as the standard $.ajax function. I'm trying to add in a standard Javascript confirm() dialog before the form gets submitted though. Problem is, as soon as you click the submit button, the ajax request goes through. How do I delay that until after the user makes a choice to confirm?

The scripts that are clashing are:

$('form').ajaxify({
        success: function(data, status, request) {
            // do some stuff
        }
    })

Which just submits the form via ajax rather than the HTML way. And then this script:

$('#accept-form').submit(function(e) {
        return confirm('Are you sure you want to accept this bid?');
});

Which does pop up the dialog, but it doesn't really matter what you click because the form has already been submitted and sent.

+1  A: 

look up the Ajaxify onStart method, I think putting the confirm there could work.

Personally, I'd use a modal window (many jQuery plugins out there, eg. jqModal) and onStart because that way everything is in your control. the JS confirm dialog can't be styled or the buttons can't be changed and you can't wire custom events to the ok/cancel buttons.

Moin Zaman
The `confirm()` window **is** a modal window! The word 'modal' means that the user **must** interact with it before continuing - it's a UI term not Javascript, so both solutions are "modal"
Yi Jiang
Regardless of which style of window I use, I'd still run into the same issue. I was thinking about re-styling it with such a plugin sometime... but it's not really a priority right now :) Anyway, I think we're referring to two completely different ajaxify plugins... does the one you linked to ajaxify forms, or just links?
Mark
@Mark: It does forms as well, see here: http://max.jsrhost.com/ajaxify/demo.php Thats the one I thought you were using. Which one are you using?
Moin Zaman
@Yi Jang: Yes, the js confirm is also a 'Modal', but I did say jQuery plugins like jqModal, which should've implied that I was referring to in-page modal window popups, not the OS style alerts / confirms. I also did not exclude or imply that a 'confirm' is not a modal.
Moin Zaman
@Moin: I did link it in the question... anyway, neither of our plugins mention anything about being able to *cancel* the request inside the `onStart` method, which is what I'd need to do...
Mark
@Moin I'm arguing about semantics now, but... if you're going to say that you'd prefer something over another, surely the other thing would not be the same as the original? Anyway, @Mark Why do you need a plugin for this? Wouldn't coding the submit event handler yourself work just as well?
Yi Jiang
@Yi Jiang: I did say why. Wiring custom stuff to the ok/cancel buttons and styling them or renaming them, which is not possible with the confirm.
Moin Zaman
@Yi: I guess I could do it myself... but it might be a lot of work. I have to nab the form action, and form method, submit it to the right place, using the right method, grab all the form data and send it along... not completely trivial to ajaxify a form!
Mark