I know this is basic but i don't know what else i can do. So i need your help. Ill tell you what happened.
The story is that i have a database request and I want to json encode the data from that request and use it through a javascript file ($.ajax
or $.json
).
the data i need from that request are two values. Latitude and longitude. but when i receive the json_encode
from php and put it in the console log or an alert it displays undefined instead of the "double precision"(because the json string).
Ill show you the code.
the php
<?php
$dbconn3 = pg_connect
("host= localhost port=5432 dbname=EmergenciesResponse user=postgres password=asdf");
$query1=pg_query
("select latitude, longitude from emergency where id_emergency=5");
$arreglo=array();
while($reg=pg_fetch_assoc($query1)){
$arreglo[]=$reg;
}
echo json_encode($arreglo);
pg_close($dbconn3);
?>
Then the .js
code
$.ajax({
url: 'consulta2.php',
data:{
},
method: 'POST',
dataType:'json',
success: function(data)
{
alert("latitude:"+data.latitude+"longitude:"+data.longitude);
}
});
As I said when I do an alert instruction the output prints undefined but the string of the php is correct. Sorry if its very easy but I don't know what to do about the undefined value
thx for your time.