Question

  $.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.

Was it helpful?

Solution

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 ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top