Passing array from php to javascript using json_encode

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
samir_gambler
Forum Newbie
Posts: 19
Joined: Wed Mar 31, 2010 9:03 am

Passing array from php to javascript using json_encode

Post by samir_gambler »

Hi,
I tried to pass an array to javascript from PHP using

Code: Select all

echo json_encode($arr_wrd);
Whine I try to access the data in JavaScript using

Code: Select all

xhr.responseText;
I get the output as

Code: Select all

["timekeeping","plover","instability","memorabilia","semidark"]
I want to treat each element as array. can anyone please help me how to do that.

Thanks & Regards,
Samir
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Passing array from php to javascript using json_encode

Post by AbraCadaver »

That is a json encoded array. You need to decode it in the javascript.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
samir_gambler
Forum Newbie
Posts: 19
Joined: Wed Mar 31, 2010 9:03 am

Re: Passing array from php to javascript using json_encode

Post by samir_gambler »

Thanks I was able to get the object using JSON.parse(xhr.responseText)
Post Reply