将json数组从PHP传输到JS

时间:2017-08-30 16:08:21

标签: javascript php jquery json

我正在尝试将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数组,应该如此。 但是当我尝试加载地图时,它给了我一些错误: enter image description here 在控制台旁边我有这个: enter image description here

进一步的信息:evry time我将数据发送到DB(带有get请求),php脚本必须向JS发送(仅)gps坐标,以便在google地图上显示它们

1 个答案:

答案 0 :(得分:1)

将您的'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);
?>