dhananjana
dhananjana

Reputation: 53

Access to JSON elements using AJAX function

I need to get data from my codeigniter controller and send it to my view as ajax call. So I use this function to send data to the ajax function

function vendorAccess(){
    $result = $this->Admin_model->allvendor();

     foreach($result as $row){
        $data = $row["joinedDate"];

        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));
         if($cont == 85){
              $response['vendorName'] = $row['vendorName'];
              echo json_encode($response);
        }

     }

}

This will send data to my ajax function. My ajax function is this

$(document).ready(function(){
 $.ajax({
     url: '<?php echo base_url();?>/Admin/vendorAccess',
     type: 'get',
     dataType:'text',
     success: function(res){
         console.log(res);

     }
 });    
});

Using this code I get the output as this

{"vendorName":"Cinnamon Grand"}{"vendorName":"Saloon Bhagya"}

Now I need to get the these two vendorName's in to my div using jquery as separate two names. I searched this problem on stackoverflow and I found some solutions. But those solutions do not work for me. One example is bellow

$(document).ready(function(){
    //$('#numOfNot').html('New');
    var tmp=null;
     $.ajax({
         url: '<?php echo base_url();?>/Admin/vendorAccess',
         type: 'get',
         dataType:'json',
         success: function(res){
             alert(res.vendorName);

         }
     });    
 });

when I use this give me alert with undefined. Thank you very much for your valuable time to spend to solve my problem

Upvotes: 0

Views: 49

Answers (2)

War10ck
War10ck

Reputation: 12508

I'd suggest a little refactoring to get this to work correctly. The JSON that you're returning is not valid. You'll want to create an array of objects in the PHP method and then, outside of your foreach() loop, echo this data back to the screen using the json_encode() method.

PHP:

function vendorAccess(){
    $result = $this->Admin_model->allvendor();
    $vendors = [];

    foreach($result as $row){
        $data = $row["joinedDate"];  
        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));

        if($cont == 85){
            $vendors[] = ['vendorName' => $row['vendorName']];
        }
    }

    echo json_encode($vendors);
}

Then, in the JavaScript, you'll want to check to ensure you're receiving an array and then loop through the array and use the data at each index in some way. In this instance, I'm logging the data to the console with console.log() but you could do any additional logic here:

JavaScript:

$(document).ready(function() {
    var tmp=null;

    $.ajax({
        url: '<?php echo base_url();?>/Admin/vendorAccess',
        type: 'GET',
        dataType:'json',
        success: function(res) {
            if(res && res.length) {
                for(var a = 0, len = res.length; a < len; a++) {
                    console.log(res[a].vendorName);
                }
            }
        }
    });    
});

Upvotes: 0

Barmar
Barmar

Reputation: 781068

You need to fill in an array in the controller, not echo JSON each time through the loop.

function vendorAccess(){
    $result = $this->Admin_model->allvendor();
    $names = array();
    foreach($result as $row){
        $data = $row["joinedDate"];

        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));
        if($cont == 85){
            $names[] = $row['vendorName'];
        }
    }
    echo json_encode(array('vendorName' => $names));
}

Upvotes: 2

Related Questions