0

I searched through this site for a solution but unfortunately, didn't worked at all so I've decided to ask you guys. Hope you help me out.

I have this PHP file that pushing values into an array.

PHP FILE:

$out_html = array();
$type1_ach = array(
    "title" => $ach_info[0], //Title1 is Value of $ach_info[0]
    "description" => $ach_info[1] //Description1 is Value of $ach_info[1]
);
array_push($out_html, $type1_ach);

$type5_ach = array(
    "title" => $ach_info[0], //Title2 is Value of $ach_info[0]
    "description" => $ach_info[1] //Description2 is Value of $ach_info[1]
);

array_push($out_html, $type5_ach);

and I echoed it using JSON since I'm passing it through jquery.

echo json_encode($out_html);

And here is my HTML File which should output the data that I passed from PHP File.

HTML FILE:

function test_pop() {
    var fb_id = "1**************"; //Facebook ID
    var v_id = "***********"; //Video ID

$.post('http://mysite.com/test.php', {fb_id: fb_id, v_id: v_id}, function(data) {
    var obj = $.parseJSON(data);
    var title = obj.title;
    var description = obj.description;
    var whole_ach = "You obtained " + title + "<br>" + description;
    console.log(whole_ach);

});
}

It outputs in console log the response is:

You obtained undefined < br > undefined

Then if I change the variables var title = obj.title; into var title = obj.title[0]; and description = obj.description into description = obj.description[0] it gives an output(response)

Uncaught TypeError: Cannot read property '0' of undefined

The desired output should be You obtained Title1 < br > Description1 but unfortunately I can't seem to do it right.

Hope you guys help me, Thanks.

0

2 Answers 2

1

You should be receiving an array of objects. So data is an array but you are treating it like an object.

Try looping over the array:

$.each(obj,function( index, item){    
   console.log( item.title);    
})

Or if you want to directly access a value from the main array:

console.log( obj[0].title);
Sign up to request clarification or add additional context in comments.

Comments

1

Need to update your JavaScript code:

function test_pop() {
    var fb_id = "1**************"; //Facebook ID
    var v_id = "***********"; //Video ID

$.post('http://mysite.com/test.php', {fb_id: fb_id, v_id: v_id}, function(data) {
    var obj = $.parseJSON(data);
    var title = obj[0].title;
    var description = obj[0].description;
    var whole_ach = "You obtained " + title + "\n" + description;
    console.log(whole_ach);

});
}

You need to specify the array index number:

    var title = obj[0].title;
    var description = obj[0].description;

Hope that helps.

1 Comment

Thanks for the answer, it also worked but couldn't accept your answer, someone answered before you. Still thanks for the efforts that you did. :)

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.