Pappa Bear, note that the original problem was that upon executing
"return true" the submit action would take the browser to a new page.
Kirov (if I understood him correctly) suggested to "wait for a result
code from the POST request before returning true". Now, in Java for
example, I would take this suggestion literally and do something like:

postRequest.wait();
return true;

Assuming that postRequest is an object which performs the POST action
and then calls this.notify() - I hope you get my drift.

In Javascript, since the concurrency model is different, there is no
such mechanism, and as you said you need to use ajax for this. So you
start an ajax request, return false, and try to simulate the effects
of "return true" in the ajax success() or error() callbacks. In our
case this means calling $(form).submit(), but I can think of more
complicated situations where the solution is not that obvious. What if
we're in the middle of a recursion? Then, you have to start talking
about stuff like "continuations" and nobody likes that.

In case my last point was not clear, consider this function:

function scanTree(subtree) {
    for (node in subtree) {
        if(! existsOnServer(node)) return;
        console.log(node);
        scanTree(subtree[node]);
    }
}

How would you implement existsOnServer with an ajax request?

Sorry for rambling,
Israel

On Jul 15, 6:34 pm, Pappa Bear <[EMAIL PROTECTED]> wrote:
> You can with ajax... depending on the result that is returned, you can
> have your script perform various things. If the result returns with a
> success for the call, you can call one thing, while if returns a
> failure, you can, say alert the user with a message. Even single
> threads allow for multiple routes. Every language has to wait for some
> result or another before continuing along its path. You can't just
> tell a program, for instance, to add 2 numbers, but continue with the
> script before it finishes. That would make no sense. Maybe I
> misunderstood your response, but that is what it sounds like you are
> saying.
>
> Also, jquery has actions for success and failure's of AJAX actions...
>
> On Jul 15, 1:54 am, iTsadok <[EMAIL PROTECTED]> wrote:
>
> > You can't "wait for a result before returning", there is no waiting
> > mechanism (only timers) and Javascript is single-threaded anyway, so
> > nothing will happen until you return.
> > What you should do is return false, and perform a manual submit of the
> > form once the POST is done. Now, this might be tricky, since just
> > calling submit() on the form will call the bound handler, thereby
> > creating an infinite loop. So you either need to unbind the handler,
> > or have an if statement and some sort of state.
>
> > As a simplified example, this should do the trick (haven't tested it,
> > sorry) for a form with id "form1":
>
> > $("#form1").bind("submit", "save_data", function() {
> >     var bound_form = $(this);
> >     $.post("/save_data.php", { name: "opt-in name", email: "opt-in
> > email" }, function() {
> >         bound_form.unbind("submit", "save_data").submit();
> >     });
>
> > });
>
> > Note that after the submit happens the handler would no longer be
> > bound to the submit event, but in your case it doesn't matter, since
> > you're going to a different page anyway.
>
> > On Jul 14, 5:59 pm, Kirov <[EMAIL PROTECTED]> wrote:
>
> > > I think you should wait for a result code from the post request before
> > > you return true; otherwise the post is handled asynchronously and
> > > perhaps not done according to your plans :)
>
> > > Good luck;
>
> > > On Jul 14, 4:49 pm, Sandy <[EMAIL PROTECTED]> wrote:
>
> > > > hi,
>
> > > > i wanna try to save the data from a form to a database before
> > > > submitting it to it's rightful "action" url. this is an opt-in form i
> > > > wanna work on.
>
> > > > i did this: $("form:last").bind("submit", submit_optin);
> > > > /* i altered the submit event of the opt-in form so it uses the
> > > > submit_optin function i created */
>
> > > > here's the submit_optin function:
> > > > var submit_optin = function(){
> > > >    var pbform = {name:"opt-in name", email: "opt-in email"}; // this
> > > > is jus a sample data to be saved.
> > > >    $.post("http://www.myurl.com/save_data";, pbform);
> > > >    return true; // return true so it submits the form to it's rightful
> > > > "action" url (which is a url from a different domain/server)
>
> > > > };
>
> > > > now for the MAIN PROBLEM... if i set submit_optin to return false it
> > > > saves the data but if i set it to return true it just submits the form
> > > > without saving the data... now is there a way to stop and wait before
> > > > it saves the data before it submits the form? or is there any way i
> > > > can achieve my goal?
>
> > > > please help, thanks :-)

Reply via email to