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

        }

    });
}
2
  • console.log(result.data[0].user_id); Commented Mar 3, 2017 at 4:50
  • I don't understand your issue. You can access the results using result.status to get the status and result.datato get the users array. What else do you want? Commented Mar 3, 2017 at 4:51

4 Answers 4

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

            }

        });
    }
Sign up to request clarification or add additional context in comments.

9 Comments

If you want to get data then use alert(obj.data[0].end_time); to get end_time in data array.
Yep bro, status is work but the data between [ ] bracket doesn't come out. :( please help me
I can't retrieve the "user_id" and any other inside data: . Please help me out bro.
use alert(obj.data[0].user_id); to get user_id inside data
Or you can use use for loop to get value in data
|
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);

5 Comments

Thanks for your reply bro but it's doesn't work both. :( I don't know why?
is your ajax successful?can you post a picture of your xhr request?
Yes bro, my ajax successful and i can alert that success result. Pls guide me how to post a picture in comment.
paste the link in the comment or update you question with the link
try something like: var data = JSON.parse(result.data);var id = data.user_id; and let me know if this works
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);
    });
});

Comments

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;

2 Comments

I did as u said, it's said like that - jquery-1.12.4.min.js:4 Uncaught TypeError: Cannot read property '0' of undefined
In that case you probably need to parse the data using JSON.parse(result) as per some of the other answers first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.