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