0

I have a function that call a POST event for a php script with 2 methods. One is call after document.ready to return an Id. The second I want to send that Id for the second method that will find all attach from that Id.

The first method will work but the second does not work. Can I create a global variable from a jquery function event, i.e.,

... function(data){
Record=Data; 
}

Is this the way to create a global variable?

My code:

function getAttachRecords(metodo, id){
    RegId = "";
    jQuery.ajax({
        type: "GET",
        async: true,
        url: "source/getAttchRecord.php",
        data:{method: metodo, id: id},
        cache: true,
        dataType: "html",
        contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
        success: function(data,textStatus,jqXHR){   

            if(metodo ="truncRecord"){  
                RegId = data;           
              $('#file_upload').uploadify({
                'formData'     : {
                            'timestamp' : '<?php echo time();?>',
                            'token'     : '<?php echo md5("unique_salt" . $timestamp);?>',
                            'registo'   : RegId             
                        },
                'uploader'  : 'source/uploadify/upload.php',
                'swf'      : 'source/uploadify/uploadify.swf',
                'script'    : 'source/uploadify/upload.php',
                'cancelImg' : 'source/uploadify/cancel.png',
                'folder'    : 'source/uploadify/uploads',
                'auto'      : true,
                'onSelect': function(){
                                    $("#table_content").slideUp();
                                },
                'onQueueComplete' : function() {
                                    $("#table_content").slideDown();
                                    getAttachRecords("getAttachRecords", RegId);  


                                }
              });
            }else{
                alert(data);
                $("table").find("#table_content").html(data);
            }
        }
    });
}
3
  • window.myfunction = function(){ ... } or window.myvar = 'foo' Commented Mar 5, 2013 at 21:19
  • You can access a variable defined in an outer scope that was previously defined. You'll have timing issues, though. Commented Mar 5, 2013 at 21:20
  • What you are currently doing creates a global variable named RegId. However, you are most likely running into a timing problem because that variable will not contain a value until the ajax request completes. Commented Mar 5, 2013 at 21:24

3 Answers 3

3

window should always be in scope, so just add your variable off of that:

//alert(bar); // `foo` is undefined up here

(function(){
  (function(){
    (function(){
      // some nested scope
      window.foo = 'bar';
    })();
  })();
})();

alert(foo); // `foo` now contains 'bar'

Just be careful though because (esp with ajax) it's not guaranteed to be available when you go to use it, so better of checking it, too:

if (typeof foo !== 'undefined'){
  alert(foo);
}

However, you can also define it before you're within that scope, too:

var foo;

(function(){
  (function(){
    (function(){
      // some nested scope
      foo = 'baz';
    })();
  })();
})();

alert(foo); // 'baz'
Sign up to request clarification or add additional context in comments.

1 Comment

i have found the problem >if(metodo ="truncRecord"){
1

Just define it initally outside of the callback or other wrapping function:

var Record;

function getNewRecord () {
   Record = 'new data';
}

That said if the value is set by a callback you will have no way to determine when this value is updated other than from within the success function or triggering some other event/callback.

Comments

0

i have found the problem

if(metodo ="truncRecord"){

this if statement is wrong it should be:

if(metodo =="truncRecord"){

After change it, everything worked fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.