1

I want to copy json object to javascript array,

{
"profiledata1":{"id":0,"music":0,"image_x":130,"image_y":155,"mouth_x":0,"mouth_y":-28.125,"active":true,"default":false},
"profiledata2":{"id":1,"music":0,"image_x":130,"image_y":155,"mouth_x":0,"mouth_y":0,"active":true,"default":false},
"profiledata3":{"id":2,"music":0,"image_x":0,"image_y":0,"mouth_x":0,"mouth_y":0,"active":false,"default":false},
"profiledata4":{"id":3,"music":0,"image_x":0,"image_y":0,"mouth_x":0,"mouth_y":0,"active":false,"default":false},
"profiledata5":{"id":4,"music":0,"image_x":0,"image_y":0,"mouth_x":0,"mouth_y":0,"active":false,"default":false},
"upload":"http:\/\/localshost\/",
"format":"jpeg","status":1 }

This is my json object returned when i call some.php through ajax, I want to copy profiledata1 to userdata_arr[0],profiledata2 to userdata_arr[1],profiledata3 to userdata_arr[2],profile4data to userdata_arr[3],profiledata5 to userdata_arr[5] in java script. My java script is as follows,

$.ajax({
    type: "POST",
    url: "some.php",
    data: {action:'load',
            id:7}

}).done(function(o) {
    var data = $.parseJSON(o);
    if (!data || data === null) {
        someError(true);

    }else{ 
        if(data.status==true){
            userdata_arr[0] = data.profiledata1[0];
            userdata_arr[1] = data.profiledata2[0];
            userdata_arr[2] = data.profiledata3[0];
            userdata_arr[3] = data.profiledata4[0];
            userdata_arr[4] = data.profiledata5[0];
            uploadDir = data.upload;
            imgFormat = data.format;


            somefunction();
        }else{
            someError(true);
        }
    }
}); 

when i execute this script i'm getting userdata_arr as undefined! Please help me to rectify this problem. I'm also attaching the some.php here,

<?php
if ($_POST['action']=='load') {
$uid=$_POST['id'];

header("content-type:application/json");
// fetch contents from db with $uid;
$query = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($query)) {
    $prof1 = $row['prof1'];
    $prof2 = $row['prof2'];
    $prof3 = $row['prof3'];
    $prof4 = $row['prof4'];
    $prof5 = $row['prof5'];
}
$jp1 = json_decode($prof1, 1);
$jp2 = json_decode($prof2, 1);
$jp3 = json_decode($prof3, 1);
$jp4 = json_decode($prof4, 1);
$jp5 = json_decode($prof5, 1);

echo json_encode($dta = array('profile1data' =>json_decode($prof1),'profile2data' =>json_decode($prof2),'profile3data' =>json_decode($prof3),'profile4data' =>json_decode($prof4),'profile5data' =>json_decode($prof5) ,'upload' =>'http://localhost/img/', 'format' =>'jpeg', 'status' =>1 )); ?>

Thanks in advance!

1 Answer 1

2

That's because you haven't declared your userdata_arr. To fix it, declare your array/object variable before using it. In your else code-block, do this:

else{ 

    var userdata_arr = {}// declare your object

    if(data.status==true){ //proceed to use your already-declared object, also notice the quote marks surrounding the object members/indexes
        userdata_arr["0"] = data.profiledata1[0];
        userdata_arr["1"] = data.profiledata2[0];
        userdata_arr["2"] = data.profiledata3[0];
        userdata_arr["3"] = data.profiledata4[0];
        userdata_arr["4"] = data.profiledata5[0];
        uploadDir = data.upload;
        imgFormat = data.format;


        somefunction();
    }else{
        someError(true);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @NaijaProgrammer

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.