0

I am using ajax query from jquery. I have 3 module or 3 file. 1st is php file of represent data , 2nd is js file where all javascipt and ajax code is written and 3rd one is my bussiness logic php file. I call a javascript function from my 1st file i.e show(id). After that this function in js file call ajax

function show(id){  

    // validation / client side.
    // function - lll to stringmatch - name proper.
    $.ajax({    
        type    : "POST",
        cache   : true,
        dataType: "json",
        url     : "a.php",
        data    : {
                    proid:id
                  },                
        success: function(data) {
         alert(data);
        //$("#match_item_display").html(data);



        }
    });



}

and my a.php file return json 2 dimensional array... After that i want to use json 2 dimmentional array in php or 1st page.

Please help me guys...

7
  • Here is alert data show only [object Object]...so on.. Commented Feb 15, 2012 at 5:29
  • Provided you wrap that in $(document).ready(), it should work fine for you. Can you elaborate on After that i want to use json 2 dimmentional array in php or 1st page.? Commented Feb 15, 2012 at 5:32
  • my index.php page have html code and php code...i call javascipt function from index page this javascript function on $(document).ready after that call javascipt function its call ajax. This ajax requestion is go on a.php and a.php return 2D array here i use echo json_encode($arr); $arr is 2D array.In ajax success funtion its return json and i want use this 2d json in index.php... Commented Feb 15, 2012 at 5:35
  • Is your problem accessing the array? Commented Feb 15, 2012 at 5:42
  • Yes i have json return in data but i want this array in javasciprt..Is this possible..??? Commented Feb 15, 2012 at 5:44

4 Answers 4

1

Yes, the data will show as [object Object] from an alert().

If you want to analyze the data as it has returned you should log it to the web console with console.log(data); instead.

You're then free to use the data in the callback. Provided you returned a json encoded object from PHP, you don't even have to massage the data. Just use data.prop1, data.prop2... etc

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

Comments

0

you cannot view the content of an object or array using alert. if you use google chrome or firefox with firebug plugins, use console.log(data); instead of alert(data);
press F12 just before you load the page to enable / show developer toolbar, you should be able to see the object in the console of the firebug or chrome.

good luck.

2 Comments

I believe F12 is for FF and IE only. See here for details. :)
Yes, F12 only works in FF (if you have firebug plugins), IE and Chrome. I've mention FF and Chrome in my answer, but I forgot mentioning IE. Thanks, Jordan.
0
var jsonData = [{"data":"oneone"},{"name":"onetwo"}];
for(var a in jsonData) {
    if(jsonData.hasOwnProperty(a)) {
        for(var b in jsonData[a]) {
            if(jsonData[a].hasOwnProperty(b)) {                                
                alert(jsonData[a][b]);
            }
        }
    }
}
​    ​

3 Comments

Its show undifined my json data is like this var jsonData = [{"data":"oneone"},{"data":"onetwo"}]
var jsonData = [{"data":"oneone"},{"name":"onetwo"}]; alert(jsonData[0].data);
how to iterate this jsonData on index.php in html code..please explain me... thanx for suggetion.. :)
0

Loop the json array

function show(id){  

    // validation / client side.
    // function - lll to stringmatch - name proper.
    $.ajax({    
        type    : "POST",
        cache   : true,
        dataType: "json",
        url     : "a.php",
        data    : {
                    proid:id
                  },                
        success: function(data) {
          var str = '<div><ul>';
         for (i=0;i<data.length;i++){
                  str += '<li>'+data[i]['json_index']+'</li>';
            }
         str +='</ul></div>';




        }
    });



}

3 Comments

how to use this data object on index.php how to implement..please discribe..thanx for suggestion.. :)
do you want to give the json values to some 'html' elements in the index.php file ?
Check the update you will have a unordered list having all the elements just put it in your page using jquery like this $('#somecontanerid').html(str);

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.