1

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);

        }

    });
}
Kyaw Zin Wai
  • 449
  • 5
  • 10
  • 26

4 Answers4

3

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] );

            }

        });
    }
Rafiqul Islam
  • 1,636
  • 1
  • 12
  • 25
  • If you want to get data then use `alert(obj.data[0].end_time);` to get end_time in data array. – Rafiqul Islam Mar 03 '17 at 05:29
  • Yep bro, status is work but the data between [ ] bracket doesn't come out. :( please help me – Kyaw Zin Wai Mar 03 '17 at 05:30
  • I can't retrieve the "user_id" and any other inside data: . Please help me out bro. – Kyaw Zin Wai Mar 03 '17 at 05:31
  • use `alert(obj.data[0].user_id);` to get user_id inside data – Rafiqul Islam Mar 03 '17 at 05:34
  • Or you can use use `for` loop to get value in data – Rafiqul Islam Mar 03 '17 at 05:56
  • I can't get the user_id, it's say undefined. here is my updated json result. {"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"}]} – Kyaw Zin Wai Mar 03 '17 at 06:33
  • I got it as your edited answer. Thanks you very much @Rafiq – Kyaw Zin Wai Mar 03 '17 at 06:37
  • I want to ask one more thing. How can i output that looping in each textbox in innerhtml? – Kyaw Zin Wai Mar 03 '17 at 07:05
  • You can use `$('admin_id').val(user_id[0]);` to set value to admin_id in the following html `` – Rafiqul Islam Mar 03 '17 at 08:54
0

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);
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

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);
    });
});
Community
  • 1
  • 1
yogesh84
  • 181
  • 1
  • 10
0

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;
Daniel Bernsons
  • 650
  • 7
  • 20