3

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").

1
  • If your PHP script works correctly and outputs anything, you should not get undefined in JavaScript. Commented Jan 23, 2013 at 13:32

3 Answers 3

20

use this

echo json_encode($array);

on server side

and

var arr=JSON.parse(result);

on client side

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

4 Comments

if I do arr[0] I get "undefined" even if, after parsing, the alert of result gave me the correct array
log into console received result and paste it here.
if I type arr in console I get: Array[1164] [0 … 99] 0: "45.4655" 1: "9.18652" 2: "45.4655" 3: "9.18652" 4: "45.4655" 5: "9.18652" 6: "45.4655" 7: "9.18652" 8: "45.4655" 9: "9.18652" 10: "45.4655" ecc. ecc.
@user1903898: Not sure, but looks like server returns array of array. $array on the server side is not what you think. Try to pass into client side some test data, e.g. echo json_encode(array('item 0', 'item 1', 'item 2', 'item 3')). And on the client side try to log arr[0]. If it works this way then you are looking for errors in wrong place.
1

As Ruslan Polutsygan mentioned, you cen use

echo json_encode($array);

on the PHP Side.

On the Javascript-Side you can simply add the DataType to the $.post()-Function:

$.post(
  './file.php',
  function( result ){    
    console.log(result);
  },
  'json'
);

and the result-Parameter is the parsed JSON-Data.

You can also set the correct Content-Type in your PHP Script. Then jQuery should automaticly parse the JSON Data returned from your PHP Script:

header('Content-type: application/json');

See
http://api.jquery.com/jQuery.post/
http://de3.php.net/json_encode

Comments

0

You need to convert the php array to json, try:

echo json_encode($array);

jQuery should be able to see it's json being returned and create a javascript object out of it automatically.

$.post('./file.php', function(result)
{    
     $.each(result, function()
     {
         console.log(this.Latitude + ":" + this.Longitude);
     });
});

1 Comment

Does jQuery parse for you?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.