1

I'm trying to extract/get data from MySQL databse and use them in javascript. I found get data from mysql database to use in javascript very useful but I'm not able to show anything (I've never used jQuery, so probably I'm missing something but I'm not able to figure out what yet)

<?php
error_reporting(0);
require 'db/connect.php';
require 'function/security.php';

$records = array();

if($result = $db->query("SELECT geoLat,geoLong FROM Stop")){
    if($result->num_rows){
        while ($row = $result->fetch_object()){
            $records[] = $row;
        }
        //$result->free();
    }
}

/*echo '<pre>', print_r($result),'</pre>';
echo '<pre>', print_r($records),'</pre>';*/
echo json_encode($records);

?>

<!DOCTYPE html>
<html>
<head>
    <title>BrindisiBus</title>
    <style>
    /* style settings for Google map */
    #map-canvas{
        width : 500px;  /* map width */
        height: 500px;  /* map height */
    }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <!--- API GOOGLE MAPS V3 -->
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
function initialize() {
    $.getJSON('paline.php', function(data) {
      $.each(data, function(fieldName, fieldValue) {
        $("#" + fieldName).val(fieldValue);
      });
    });
    /*
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Hello World!'
  });
*/
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>
<body>
    <div id='map-canvas' ></div><br/>
    <p id="total"></p>
    <p id="fieldName"></p>
</body>
</html>

With the query I get longitude and latitude, than I should save that values in js and create markers on the map. Is it possible to do everything in the same file? How could I check if getJSON is working at least and if it's giving errors?

8
  • Do you have elements having ids geoLat and geoLong in HTML? Commented Jun 2, 2015 at 12:05
  • geoLat and geoLong are field of a table of the database Commented Jun 2, 2015 at 12:08
  • $("#" + fieldName).val(fieldValue); here fieldName must exists in HTML Commented Jun 2, 2015 at 12:09
  • Do you mean <p id="fieldName"></p> ? I tried also with <input type="text" id="fieldName" /> but I don't get result Commented Jun 2, 2015 at 12:13
  • I mean <p id="geoLat"></p> and <p id="geoLong"></p> try with this Commented Jun 2, 2015 at 12:16

1 Answer 1

2
    <?php
    error_reporting(0);
    require 'db/connect.php';
    require 'function/security.php';

    $records = array();

    if($result = $db->query("SELECT geoLat,geoLong FROM Stop")){
        if($result->num_rows){
            while ($row = $result->fetch_object()){
                $records[] = $row;
            }
            //$result->free();
        }
    }

    /*echo '<pre>', print_r($result),'</pre>';
    echo '<pre>', print_r($records),'</pre>';*/
    $data=json_encode($records);

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>BrindisiBus</title>
        <style>
        /* style settings for Google map */
        #map-canvas{
            width : 500px;  /* map width */
            height: 500px;  /* map height */
        }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <!--- API GOOGLE MAPS V3 -->
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script>
var data=<?php echo $data; ?>
    function initialize() {

          $.each(data, function(fieldName, fieldValue) {
            $("#fieldName").val(fieldValue.geoLat);
          });

        /*
      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var mapOptions = {
        zoom: 4,
        center: myLatlng
      }
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Hello World!'
      });
    */
    }

    google.maps.event.addDomListener(window, 'load', initialize);

        </script>
    </head>
    <body>
        <div id='map-canvas' ></div><br/>
        <p id="total"></p>
        <p id="fieldName"></p>
    </body>
    </html>
Sign up to request clarification or add additional context in comments.

2 Comments

Nothing is show in fieldName, but now var data in javascript contain the object
come on chat i will explain you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.