2

I need to send a json array from a php script to Javascript.

 <?php //controllo se sono presenti i parametri if(isset($_GET['ID_utente']) && isset($_GET['Longitudine']) && isset($_GET['Latitudine'])) { //Recupero il valore dei parametri $ID_utente = $_GET['ID_utente']; $Longitudine = $_GET['Longitudine']; $Latitudine = $_GET['Latitudine']; } //eseguo la connessione al database sul server locale //inserendo nome utente e password $conn = mysql_connect('localhost', 'realegr', 'rabredugbo19'); //gestione degli errori // if (!$conn) {die('Impossibile connettersi: ' . mysql_error());} //seleziono il databse mysql_select_db("my_realegr") or die( "Impossibile selezionare il database."); //creo una stringa sql di inserimento con i valori //recuperati dall'url $sql = "INSERT INTO `my_realegr`.`DatiSinistro` ( `ID_sinistro` , `Tempo_Server` , `Tempo_Locale` , `ID_utente`, `Longitudine`, `Latitudine` ) VALUES ( NULL , CURRENT_TIMESTAMP , NULL , '" . $ID_utente . "', '" . $Longitudine . "', '" . $Latitudine . "') "; $q = "SELECT Longitudine, Latitudine FROM DatiSinistro ORDER by ID_sinistro DESC LIMIT 1"; $result = mysql_query($q, $conn); if($row = mysql_fetch_assoc($result)) { echo json_encode([ 'status' => true, 'latitude' => $row['Latitudine'], 'longitude' => $row['Longitudine'], ]); } //gestione degli errori if(! $result ){die('Impossibile eseguire la query: ' . mysql_error());} //chiudo la connessione al db mysql_close($conn); ?>

<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBkEtPH07_xBVh8dWBzCQYtWimkOUnPGMQ&callback=initMap"></script> <style> #map { height: 400px; width: 100%; } </style> </head> <body> <h3>My Google Maps Demo</h3> <div id="map"></div> <script> function initMap(){ var $request = $.get('http://realegr.altervista.org/ProvaReale1.php'); $request.done( function(data) { alert(data); var pasedData = JSON.parse(data); alert(pasedData.latitude); var uluru = {lat: pasedData.latitude, lng:pasedData.longitude}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: uluru }); var marker = new google.maps.Marker({ position: uluru, map: map }); }) } </script> </body> </html>

Output of php script:enter image description here

When I load Maps,alert(data) shows me : {"status":true,"latitude":"45.062583","longitude":"7.662160"}

So PHP script seems work well and besides Javascript receives JSON array With "var pasedData = JSON.parse(data); " do I obtain an array parsed?? When I try to show the content with alert(pasedData[0]) it shows me "undefined".What's wrong?

2
  • use pasedData.status, pasedData.latitude and pasedData.longitude Commented Aug 24, 2017 at 10:37
  • hi dear your out put not in JSON array {"status":true,"latitude":"45.062583","longitude":"7.662160"} if your output like this [{"status":true,"latitude":"45.062583","longitude":"7.662160"}] then you can use like this pasedData[0] Commented Aug 24, 2017 at 10:39

2 Answers 2

1

use pasedData.status, pasedData.latitude and pasedData.longitude

UPDATED

Please use this code

<script> function initMap(){ // } </script> 

and place your code of initMap inside this function.

UPDATE 2

Please use this code

var pasedData = JSON.parse(data); var longi = parseFloat(pasedData.longitude); var lati = parseFloat(pasedData.latitude); var uluru = {lat: lati, lng:longi}; 
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you.But now in console I have a new error: fc {message: "initMap is not a function", name: "InvalidValueError", stack: "Error↵ at new fc (maps.googleapis.com/m…_xBVh8dWBzCQYtWimkOUnPGMQ&callback=initMap:136:68"} any hint?? (I have changed the script)
Do you have a function initMap() in your script tag? It is javascript function.
I updated the script (If I understood what u meant ). Now it gives me these errors: InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number; InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number
Can you please update your code in your question so that I can see?
Already done. Anyway it's strange because the values are already parsed :S
|
0

Access with . operator in place of trying it like array index. It will Work. Can try below example in browser console.

var data = '{"status":true,"latitude":"45.062583","longitude":"7.662160"}'; var t = JSON.parse(data); console.log(t.status); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.