0

I need to be able to use a variable I am setting from the Ajax success data outside of the Ajax request block.

These are my variables set above the below code

 ruleID     = value['_id'].$id;
 ruleName   = value['rule_name'];
 ruleDesc   = value['rule_desc'];
 ruleType   = value['vali_type'];
 folderID   = value['folder'];
 var folderName

This is my Ajax request

 $.ajax({
    type: "POST",
    url: "../ajax/get_folder_name.php",
    data: {
        folderID: folderID
    }, 
    success: function(data) {               
        $.each(data, function(index, value){
            folderName = value.folder_name;
            console.log(folderName);
        })
    },
});

This is where I need to be able to use the variable folderName (this code is directly below the Ajax request)

 if(ruleErrors[ruleID] !== undefined ){
    var errorCount = ruleErrors[ruleID];

    var rule = "<div class='panel panel-default'>";
    rule += "<div id='"+ruleID+"' class='panel-heading cursor-pointer panel-hover'>";

THIS LINE IS WHERE I NEED TO USE folderName --

 //rule += "<h4>"+ruleName+"</h4><h5>"+ruleDesc+"( "+folderName+" )</h5><h1 style='float:right; margin-top:-55px;'><span class='label label-danger ruleCount "+ruleID+"'>"+errorCount+"</span></h1>";
    rule += "</div>"; //- Close panel-heading
    rule += "<div class='panel-body "+ruleID+"' style='display:none;'>";
    rule += "<div class='"+ruleID+"table-responsive"+" spinner' style='text-align: center; font-size: 20px;' class='welcome-text'><i class='fa fa-spinner fa-2x fa-spin'></i></div>";
    rule += "<table class='"+ruleID+" table' style='display:none'><thead><tr>";
    rule += "<th>Passenger Name (ID)</th><th>Consultation</th><th>Account Name (ID)</th><th>Tran Type</th><th>Checked Value</th><th>Reason</th><th></th>";
    rule += "</tr></thead><tbody class='"+ruleID+"'></tbody></table>";
    rule += "</div>"; //- Close panel-body
    rule += "</div>"; //- Close panel

    $("#rules").append(rule);
 }

It turns out I cant use it in the Ajax success block, I can't think any other way to get information in.

Kieron606
  • 613
  • 3
  • 13
  • 28
  • 1
    That's an asynchronous problem. It's wide treated here in stackoverflow. Just use the search function to see how many answers about this there are. Otherwise, you can make a working example, instead of share that pieces of code, that we can't imagine the workflow. – Marcos Pérez Gude Mar 10 '16 at 12:13
  • 2
    Have you tired putting the code that uses `folderName` in the success function? – Stephen O'Connor Mar 10 '16 at 12:32
  • Thanks @SteveO'Connor went straight over my head, never thought of that. – Kieron606 Mar 10 '16 at 12:42

0 Answers0