Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  28] [ 5]  / answers: 1 / hits: 17773  / 12 Years ago, wed, january 23, 2013, 12:00:00

I need to get an array generated from a script php. I have Latitude and Longitude for each user in a database.
I take the values from the db with this code (file.php):



$query = SELECT Latitude, Longitude FROM USERS;
$result=mysql_query($query);
$array=array();
while ($data = mysql_fetch_array($result)) {
$array[]=$data['Latitude'];
$array[]=$data['Longitude'];
}

echo $array;


and I call with ajax with this code:



$.post('./file.php',
function( result ){
alert(result);
});


but even if in the script php the array is correct (if I echo array[25] I obtain the right value) in Javascript I obtain Undefined.
How can I get the array in correct way??
thanks!



edit: after encoded with json_encode($array); in php and JSON.parse(result) in javascript seems not working.
In the console I have the array, but I can't access to its values. (Array[0] gave me undefined).


More From » php

 Answers
1

use this



echo json_encode($array);


on server side



and



var arr=JSON.parse(result);


on client side


[#80677] Tuesday, January 22, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frankiebobbyc

Total Points: 18
Total Questions: 85
Total Answers: 104

Location: Norway
Member since Wed, Jul 7, 2021
3 Years ago
;