Kyaw Zin Wai
Kyaw Zin Wai

Reputation: 459

How to output json array value in ajax success?

I have post the data and return the value with json_encode and get that in ajax success stage. but i can't out that data value in specific input. Here is my html input. The return value are show in console and alert box as below.

{"status":"0","data":[{"user_id":"1","start_time":"00:00:00","end_time":"01:00:00","date_select":"2017-03-23","admin_flag":"0","interview_plan_staff_id":"1","interview_plan_staff_name":"Administrator","user_name":"\u304a\u306a\u307e\u30481"},{"user_id":"31","start_time":"00:00:00","end_time":"01:00:00","date_select":"2017-03-23","admin_flag":"0","interview_plan_staff_id":"1","interview_plan_staff_name":"Administrator","user_name":"uchida"}]}

<input type="text" id="admin_id" class="form-control">

Here is my ajax

function cal_click(cal_date){
   var calDate = cal_date
   var date_format = calDate.replace(/-/g, "/");
   var base_url = <?php base_url(); ?>
   $.ajax({
    type: "post",
    url: "<?php echo base_url('Admin_top/getcal');?>",
    data: {calDate:calDate},
    cache: false,
    async: false,
    success: function(result){

            console.log(result);
            alert(result);

        }

    });
}

Upvotes: 1

Views: 14521

Answers (4)

Rafiqul Islam
Rafiqul Islam

Reputation: 1646

Use JSON.parse to get specific input from result

    function cal_click(cal_date){
       var calDate = cal_date
       var date_format = calDate.replace(/-/g, "/");
       var base_url = <?php base_url(); ?>
       $.ajax({
        type: "post",
        url: "<?php echo base_url('Admin_top/getcal');?>",
        data: {calDate:calDate},
        cache: false,
        async: false,
        success: function(result){

                console.log(result);
                var obj = JSON.parse(result);
                alert(obj.status);
                //alert(result);
                var user_id = [];
                var start_time = [];
                for (i = 0; i < obj.data.length; i++) {
                   user_id[i] = obj.data[i].user_id;
                   start_time[i] = obj.data[i].start_time;
                }
             alert(' First user '+user_id[0]+' Second User '+ user_id[1]+' First start_time '+start_time[0]+' Second start_time '+ start_time[1] );

            }

        });
    }

Upvotes: 3

Daniel Bernsons
Daniel Bernsons

Reputation: 650

When in an AJAX callback, you can use result.data to access the array of objects being returned. You can work with these like you would any other Javascript object. You may need to deserialize the JSON first.

To accomplish what you're trying to do, the following code would do the trick, although it will only use the very first object in the array as you only have one text box.

var responseObj = JSON.parse(result);
document.getElementById('admin_id').value = responseObj.data[0].user_id;

Upvotes: 0

yogesh84
yogesh84

Reputation: 181

Read Following posts you will get idea about json parsing

Parse JSON from JQuery.ajax success data

how to parse json data with jquery / javascript?

and you can try looping like this

var parsedJson  =   $.parseJSON(json);
$(parsedJson).each(function(index, element) {
    console.log(element.status);
    $(element.data).each(function(k,v) {
        console.log(v.user_id);
    });
});

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32344

Use a each loop to get the ids,result is a object that has a data array:

$.each(result.data,function(i,v){
     console.log(v.user_id);
     //$('.admin_id').val(v.user_id);//use val to append the value, note you have multiple ids so you need multiple inputs 
});

if this doesn't work then you return a string not json so you need to convert it to json using:

var result = JSON.parse(result);

Upvotes: 0

Related Questions