I am trying to transfer a json array from PHP to JS
<?php //controllo se sono presenti i parametri if(isset($_GET['id_utente']) && isset($_GET['longitude']) && isset($_GET['latitude'])) { //Recupero il valore dei parametri $id_utente = $_GET['id_utente']; $longitude= $_GET['longitude']; $latitude = $_GET['latitude']; } $servername = "localhost"; $username = "realegr"; $password = "pass"; $dbname = "my_realegr"; // Create connection, $conn = mysqli_connect($servername, $username, $password,$dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully\\n"; // Insert Data $sql = "INSERT INTO Prova (id_utente, latitude, longitude) VALUES ('" . $id_utente . "', '" . $longitude . "', '" . $latitude . "') "; if (mysqli_query($conn, $sql)) { echo "New record created successfully\\n\\r"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } // Execute query and save it in result $sql = "SELECT latitude,longitude FROM Prova ORDER by reg_date DESC LIMIT 1"; $result = mysqli_query($conn, $sql); //if (mysqli_num_rows($result) > 0) { // output data of each row /* while($row = mysqli_fetch_assoc($result)) { echo "latitude: " . $row["longitude"]. " - longitude: " . $row["latitude"]. "<br>"; } } else { echo "0 results"; } */ $row = mysqli_fetch_assoc($result); echo json_encode([ 'status' => true, 'latitude' => $row["longitude"], 'longitude' => $row["latitude"], ]); mysqli_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/PopolamentoTabella.php'); $request.done( function(data) { alert(data); var pasedData = JSON.parse(data); var longi = parseFloat(pasedData.longitude); var lati = parseFloat(pasedData.latitude); var uluru = {lat: lati, lng: longi}; 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>
further information:evry time I send data to DB( with a get request), the php script has to send (only)gps coordinates to JS,to show them on a google maps
Change your 'PopolamentoTabella.php' to this
<?php
$servername = "localhost";
$username = "YOURUSERNAME";
$password = "YOURPASSWORD";
$dbname = "YOUR DB";
// Create connection,
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully\n";
//controllo se sono presenti i parametri
if (isset($_GET['id_utente']) && isset($_GET['longitude']) && isset($_GET['latitude'])) {
//Recupero il valore dei parametri
$id_utente = $_GET['id_utente'];
$longitude = $_GET['longitude'];
$latitude = $_GET['latitude'];
// Insert Data
$sql = "INSERT INTO Prova (id_utente, latitude, longitude)
VALUES ('" . $id_utente . "', '" . $longitude . "', '" . $latitude . "')
";
if (mysqli_query($conn, $sql)) {
//echo "New record created successfully\n\r";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
} else {
// Execute query and save it in result
$sql = "SELECT latitude,longitude FROM Prova ORDER by reg_date DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
//if (mysqli_num_rows($result) > 0) {
// output data of each row
/* while($row = mysqli_fetch_assoc($result)) {
echo "latitude: " . $row["longitude"]. " - longitude: " . $row["latitude"]. "<br>";
}
} else {
echo "0 results";
}
*/
$row = mysqli_fetch_assoc($result);
echo json_encode([
'status' => true,
'latitude' => $row["longitude"],
'longitude' => $row["latitude"],
]);
}
//echo $longitude;
mysqli_close($conn);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.