1

I want to pass two variable of json to ajax from controller here I have little code of it.

echo json_encode($qry1);
echo json_encode($qry2);

How can I get this both in ajax and How can I use it like data.qry1 and data.qry2.

    $.ajax({
        url:"<?php echo base_url(); ?>getdata",
        type: "POST",           
        dataType: 'json',
        data:{Paper_name : p_name},

        success : function(data){                   


            if(data != ""){                 
              alert(data.qry1);

            }else{                                  

             alert(data.qry2);
       },
       error : function(data){

        alert(data.qry2);
       }
    });

2 Answers 2

2

Merge both independent array to one single array

$dataArray = array(
    'qry1' => $qry1,
    'qry2' => $qry1
);

echo json_encode($dataArray);

In ajax, add console.log(data) and check how its comming

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

3 Comments

is it possible to get length of this. like data.qry1.length.?
Yes can. individual array can be get length
@Mahi happy to help :)
2

Put the two variables into an associative array:

$qry = array(
    'qry1' => $qry1,
    'qry2' => $qry2
);

echo json_encode($qry);

3 Comments

is it possible to get length of this. like data.qry1.length.?
It should be, yes... assuming that's javascript :-)
Aww man, I was faster by 20 sec, lol. Abdulla, you get an upvote from me :-)

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.