1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Weird problem in callback / $.post

Discussion in 'jQuery' started by PoPSiCLe, Aug 3, 2014.

  1. #1
    I have the following function:
    
    $(function() {
         var userID = $('#user_id').text();
         $.post('contracts.php', { 'user_id':userID }, function(data) {
           var markup = '';
    
           if (data == 0) {
             $('#overlay').fadeIn();
             $('#editbox').fadeIn();
    
             markup = [
                 '<form id="sign_contractform_'+userID+'" method="post">',
                 '<h1 id="sign_contract_form_heading">'+getDefinedLangConst("__SIGNCONTRACTHEADING",'',"sign_contract_form_heading",'')+'</h1>',
                 '<div class="contract_text">Du må signere kontrakt før du får tilgang til siden</div>',
                 '<label for="default_contract">Aksepter standardkontrakt</label><input type="checkbox" id="default_contract" value="1" name="default_contract">',
                 '<input type="submit" name="submitcontract" disabled="disabled" id="submitcontract" value="'+getDefinedLangConst("__SUBMITCONTRACT",'',"submitcontract",'val')+'">',
                 '</form>'
               ].join('');
             $(markup).appendTo('#editbox').fadeIn();
           }
    
           $('#sign_contractform_'+userID+' input[type=checkbox]').on('click', function() {
             $('#submitcontract').prop('disabled',false);
             $('#submitcontract').on('click', function() {
               var defaultContract = $('#default_contract').val();
               $.post('contracts.php', { 'defaultcontract' : defaultContract,'user_id' : userID }, function(updatedata) {
                 console.log(updatedata);
               })
             })
           })
         })
       })
    
    Code (markup):
    What this does (or, at least is supposed to do, is check for a signed contract (the first $.post, and if no contract is found, and signed, display the form) - this works.
    However, the second $.post doesn't work, for some reason. It posts okay, but in Firebug it shows in red - like if the file wasn't there (404) or something similar - but it doesn't show anything - no error messages, nothing. Nor does it display a "return" - which one would assume it would show. The contracts.php-file has output no matter what result you're getting, so it should definitely show something.

    Anyone have any clue?
     
    PoPSiCLe, Aug 3, 2014 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    I think you need to move the submit out of the checkbox click function.

    
    $(function() {
         var userID = $('#user_id').text();
         $.post('contracts.php', { 'user_id':userID }, function(data) {
           var markup = '';
    
           if (data == 0) {
             $('#overlay').fadeIn();
             $('#editbox').fadeIn();
    
             markup = [
                 '<form id="sign_contractform_'+userID+'" method="post">',
                 '<h1 id="sign_contract_form_heading">'+getDefinedLangConst("__SIGNCONTRACTHEADING",'',"sign_contract_form_heading",'')+'</h1>',
                 '<div class="contract_text">Du må signere kontrakt før du får tilgang til siden</div>',
                 '<label for="default_contract">Aksepter standardkontrakt</label><input type="checkbox" id="default_contract" value="1" name="default_contract">',
                 '<input type="submit" name="submitcontract" disabled="disabled" id="submitcontract" value="'+getDefinedLangConst("__SUBMITCONTRACT",'',"submitcontract",'val')+'">',
                 '</form>'
               ].join('');
             $(markup).appendTo('#editbox').fadeIn();
           }
    
           $('#sign_contractform_'+userID+' input[type=checkbox]').on('click', function() {
             $('#submitcontract').prop('disabled',false);
           })
             $('#submitcontract').on('click', function() {
               var defaultContract = $('#default_contract').val();
               $.post('contracts.php', { 'defaultcontract' : defaultContract,'user_id' : userID }, function(updatedata) {
                 console.log(updatedata);
               })
             })
         })
       })
    
    Code (markup):
    Not tested.
     
    HuggyStudios, Aug 4, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Hm. Maybe. I'll test tomorrow.
     
    PoPSiCLe, Aug 4, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    No difference, whatsoever. It seems there's a problem calling the same file again, from within the $.post - or maybe I'm just doing something very weird in the receiving .php-file, that I just can't see...
     
    PoPSiCLe, Aug 6, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    And... I dunno what I did, really, but now it's working... *headdesk*
    Updated code:
    
      $(function() {
         var userID = $('#user_id').text();
    
         if (userID != '' || userID != 0) {
           $.post('contracts.php', { 'user_id':userID }, function(data) {
             data = $.parseJSON(data);
    
             if (data.content == 'no contract signed') {
               var markup = '';
               
               $('#overlay').fadeIn();
               $('#editbox').fadeIn();
    
               markup = [
                   '<form id="sign_contractform" method="post" action="contracts.php">',
                   '<h1 id="sign_contract_form_heading">'+getDefinedLangConst("__SIGNCONTRACTHEADING",'',"sign_contract_form_heading",'')+'</h1>',
                   '<div class="contract_text">Du må signere kontrakt før du får tilgang til siden</div>',
                   '<label for="default_contract">Aksepter standardkontrakt</label><input type="checkbox" id="default_contract" value="1" name="default_contract">',
                   '<input type="submit" name="submitcontract" disabled="disabled" id="submitcontract" value="'+getDefinedLangConst("__SUBMITCONTRACT",'',"submitcontract",'val')+'">',
                   '</form>'
                 ].join('');
    
               $(markup).appendTo('#editbox').fadeIn();
         
               $('#sign_contractform input[type=checkbox]').on('click', function() {
                 $('#submitcontract').prop('disabled',false);
               })
    
               $('#sign_contractform input[type=submit]').on('click', function(evt) {
                 evt.preventDefault();
    
                 var userID = $('#user_id').text();
                 var defaultContract = $('#default_contract').val();
    
                 if (userID != '' && defaultContract == 1) {
                   $.post('contracts.php', { 'defaultcontract' : defaultContract, 'user_id' : userID }, function(updatedata) {
                     data = $.parseJSON(updatedata);
                     showUpdateInfo(''+data.content+'',''+data.infotype+'');
                     overlayHide();
                   })
                 }
               })
             } else {
               // do nothing
               console.log('do nothing');
             }
           })
         }
       })
    
    
    Code (markup):
     
    PoPSiCLe, Aug 6, 2014 IP
  6. GORF

    GORF Well-Known Member

    Messages:
    224
    Likes Received:
    21
    Best Answers:
    3
    Trophy Points:
    165
    #6
    Browsers can act funny when you are making changes to JavaScript files. Sometimes they just won't show anything different when you are editing a file (especially locally). The safest bet is close all browsers and clear their cache.
     
    GORF, Aug 6, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    That is true, but I'm quite sure that was not the issue here - viewing the source code and accessing the js-files and others showed the correct information being present. I haven't really gone through the differences between the two versions of code, but I might have changed some value somewhere, added an else here, moved around a couple things there, and ended up with something that works as it should, so I'm more or less happy :)
     
    PoPSiCLe, Aug 6, 2014 IP
  8. Alexstorm

    Alexstorm Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #8
    Post within a post inside an if raises some Firefox security issue? Or the way Firefox processes the IF no userID and DefaultContract ==1 takes a long time depending on the dbase membership size? Could saving results of if functions to a variable and use the variable to process the logic let Firefox handle it better?
     
    Alexstorm, Mar 12, 2015 IP