views:

1451

answers:

5

Hello,

I have a confirm step in one of my pages.

Desired action:

  • user clicks 'submit' and an AJAX request is made that does the appropriate action and returns a confirm dialog

  • user 'confirms' and then the form is submitted using a standard post.

So for the first part I'm fine using the jQuery form plugin:

$('form').ajaxForm(...options...);

For the second part I'd like to submit the form again, but non-ajax. Basically I want to do this:

$('form').submit();

And have it do an actual browser post. The problem is that $('form').submit() just triggers the ajax submit.

Is there a way to use the form for both purposes?

A: 

Can't you just unregister the submit event handler after you've ajax-posted the results? Why do you need to post the same data twice, by the way? If the data haven't changed between the Ajax post and the regular one, why is the regular one needed?

asbjornu
Two requirements:1) a confirm step is needed2) I have to use the existing dbSo I'm doing 99% of the same work in both requests, but only the 'real' request persists it to the database. Ideally I would save the work from the first request and the second request would just need to reference that saved work, but a) I don't want to rely on session and b) I can't create a new table for persistence of the request.
eyston
A: 

You can try to change a value in the form (se some hidden value to 1), do another ajax request and finally do a redirect. It's not the same but it should work. Note that it's very strange to submit the same data twice though..

Keeper
Its a confirm step ... it does everything but save it to the database, so its the exact same data.
eyston
and I still don't get why you should post twice...
Keeper
A: 

Answered by Surya as a comment (if you check this question again please post the answer so I can mark it!)


$('forms-submit-button').click() ..does that work , for the second submit?

eyston
+1  A: 
$('forms-submit-button').click()

..does that work , for the second submit?

:) :)

Surya
A: 
form onsubmit='ajaxCheck();'
...  
/form script var ajaxCheck = function() { //do check return confirm(); // if ok form submit normaly / if cancel form doesn't submit } /script

or something with a flag:

var flag = true;

var firstCheck = function()
{
     if( flag )
     {
       //do the ajax Call which will fire an event,
       // let's call it onData
       $.post(url,{param1:val1,...,paramN:valN},onData);
       return false;
     }
     return true;
}

var onData = function (data)
{
       flag = !confirm(...); 
      //if user click ok and try to re-submit the form
      //this time will just go
}