簡體   English   中英

將JSON數組從PHP傳輸到JS

[英]Transfer json array from PHP to JS

我正在嘗試將json數組從PHP傳輸到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> 
php腳本似乎運行良好。它將更新數據庫並按需回顯JSON數組。 但是當我嘗試加載地圖時,它給了我一些錯誤: 在此處輸入圖片說明 在控制台旁邊,我有這個: 在此處輸入圖片說明

進一步的信息:每當我將數據發送到DB(帶有get請求)時,php腳本必須將(僅)gps坐標發送給JS,以在Google地圖上顯示它們

將您的“ PopolamentoTabella.php”更改為此

<?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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM