views:

240

answers:

1

Hello,

I have a problem within this code. I want to choose whether I will submit the form or not based on the outcome off the ajaxcall.

If I hardcode it with return false; It stops going to the paypal site. That's why I want it to depend on a variable set in the ajax succes function.

It is probably lost in the callback, so that's why I ask for help.

This is the code:

$("#img_download").click(function(e){

 $("#main_content").find('span').not('#loading').remove();
 loading.fadeIn(1000);
 $("#main_content").load("../../../includes/snip.php #betaalbox", function(){
  var invoerGebruikersnaam = $("#gebruikersnaam");
  var invoerEmail = $("#email");
  loading.hide();

  if($("#uitloggentaxibel").size()!=0){
   //alert('we zijn ingelogd');
   $('#idgebruikerbox').hide();
   ingelogd=1;
  }else{
   invoerEmail.focus();
   //alert('we zijn niet ingelogd');
  }

   $('form#paypalio').submit(function(){ 
   if(invoerGebruikersnaam.attr("value") && invoerEmail.attr("value")){
     alert('is gecheckt, check ook op geldig emailadres');
    //check ook voor een geldig emailadres
    //OK, dan...
    //vergelijk met db gegevens
    gebruikersnaam = invoerGebruikersnaam.attr("value");
     email = invoerEmail.attr("value");
    $.ajax({
    url: "/includes/pdt_paypal.php",
    data: ({gebruikersnaam: gebruikersnaam, actie: "idgebruikercheck", email: email}),
      cache: false,
    type: "POST",
      dataType: "json",
    timeout: 5000,
    success: function(data,textStatus){
    //$('#main_content').html(data.responseText);
    //sla geretourneerde gegevens bij gebruiker op
    identificatie=data.check;
    if(identificatie=="ok"){
     customstring=email;

     $('.bericht.idgebruikerfout').html('identificatie was ok').show();
    }else{
     $('.bericht.idgebruikerfout').html('identificatie heeft gefaald').show();
     annuleerPayPal="ok";
    }

    }//EINDE success
     ,error: function(XMLHttpRequest, textStatus, errorThrown) { 
      if(textStatus == 'timeout') {
       //doe iets
      }else if (textStatus == 'error'){
       //doe iets
      } 
     }//EINDE error

    });//EINDE ajax
    //ZOJA, dan...

    }else{

      $('.bericht.idgebruikerfout').html(idfout2).show();
      return false;
    }
    if(annuleerPayPal=="ok"){return false;}  

     //return false;  
  });//EINDE submit
 });//EINDE load

});

EDIT BECAUSE

I solved it by removing the logic off setting a variable, because it get's lost. My best guess is that the different callbacks return to a different context.

Anyway, by being a bit more direct, by evaluating an if/else statement block within one context(callback), I simply Instruct to submit the form. $('form').submit();

solved

thanks, Richard

+2  A: 

Add this somewhere around the top:

  var SubmitForm = false;

Then add this after it pass validation:

if(identificatie=="ok"){
       SubmitForm = true;  //<--- this part
       customstring=email;

Then return that variable instead of returning flat true or false;

   return SubmitForm;

Quick edit:

I assumed you want to submit the form based on what the ajax request returned, not on wheter it was simply successful. The same idea applies, you just need to add the:

       SubmitForm = true;

Before the if(identificatie=="ok"){ instead of after, so that it gets turned to true right after the request is a known success.

Anthony
thanks for the help, I edited my answer
Richard