0
  $.ajax({
             url:"<?php echo site_url('proposal/getContact');?>/"+client,
             type: 'GET',
             dataType: 'json',
             success:function(data) {
             //On succes the data is returned from the url and used accordingly.
                htm='<option value="">Select</option>';
                for(i=0;i<data.length;i++){
                    htm +='<option value="'+data[i].Id+'">'+data[i].Name+'</option>';
                }
                 $('select[id="ProjectContact"]').html(htm);
              }
          });

I want to use the variable data later so that when i select the project contact i have to deal with the same data. I have tried using

$('select[id="ProjectContact"]').change(function() {
    var contact=$('select[id="ProjectContact"]').val();
                 htm='<div class="span2 text-right"> <img class="img-rounded"> </div>';
                 htm+='<div class="span4">';
                 htm+='<strong>';
                 for(i=0;i<data.length;i++){
                    if(data[i].Id != contact)
                        continue;
                    htm+=data[i].Name+
                    "</strong>,<br/>"+data[i].Designation+",  <br/>"+
                    data[i].PhoneNumber+",<br />";
                    htm+=data[i].Email+",<br/>"+
                    data[i].FacebookUrl+",<br />"+data[i].TwitterUrl+",<br/>"+
                    data[i].LinkedinUrl;  
                    htm+='</div>';
                 }
                 $("#ContactDetails").html(htm); 

 });

says data is not recognised. Thank You.

spod
  • 406
  • 3
  • 5
  • 19

1 Answers1

1

You need to store the data in a variable outside the scope of the success function:

 var myData= {};
 $.ajax({
         url:"<?php echo site_url('proposal/getContact');?>/"+client,
         type: 'GET',
         dataType: 'json',
         success:function(data) {
             myData.data = data;
         }
  });

You should then be able to refer to myData later. However, how does your later code know that the ajax call has completed ?

SteveP
  • 18,840
  • 9
  • 47
  • 60
  • I need the data only when the ajax call is successful and i dont have to bother about its failure.The data i am using is out of the scope from ajax success(function(){}).I want it to used out of this function as it is a different event selection. Let me try use the above method.Thank You – spod Mar 26 '13 at 09:42
  • I tried to save the data into ContactData =data and used it later. I have received this error saying "Uncaught ReferenceError: ContactData is not defined " – spod Mar 26 '13 at 09:44
  • Uncaught TypeError: Cannot read property 'length' of undefined – spod Mar 26 '13 at 09:51
  • It depends on the scope of the various bits of code. You haven't posted enough code to give a 100% answer. Make sure that ContactData is declared somewhere which is accesible to both pieces of code. – SteveP Mar 26 '13 at 10:10
  • except for myData.data=data; everything is correct and yeah I accepted it. Thank you Steve – spod Mar 26 '13 at 11:21
  • @phpLearner- I am stuck in the same problem. What did you use in place of myData.data = data? Please help! – Chirag Mongia Dec 09 '13 at 19:20