0

I have a database of latitudes and longitudes. I am using java script and Google Maps API to generate a heatmap of this information. I am trying to populate the javascript array using PHP. Currently, I am doing this:

alumdata = [ $.ajax({
            url: 'Major.php',
            type: 'POST'
            data: { major : major },
            success: function (data) {  
            }
        })
    ];

Major.php is the PHP file that will populate the Javascript array, alumdata. I am basically trying to add Google maps objects into every element of the JS Array and calling the initalize function so that the locations on the heatmap change according to the value of the variable major. In my Major.php file, I do this:

<?php   
require 'connectDB.php';
$major = $_POST['major'];


if ($major == 'All'){
    if(!$entry=$db->query("SELECT * FROM trial"))
        die('There was an error connecting: queryError [' . $db->error . ']');

}
else{
    if(!$entry=$db->query("SELECT * FROM trial WHERE Major = '$major'"))
        die('There was an error connecting: queryError [' . $db->error . ']');

}

while($foobar=$entry->fetch_assoc()){
    $longitude = $foobar['Longitude'];
    $latitude = $foobar['Latitude'];
print <<<HTML2
      new google.maps.LatLng($latitude, $longitude),
HTML2;
}
?>

However, nothing is displayed. I have been messing around with this for a while and cannot find a solution. I would appreciate any suggestions. Thank you.

  • 3
    you are not doing anything with `data` in your ajax success callback, [read the docs](http://api.jquery.com/jQuery.ajax/) on how to properly use the ajax function – Patrick Evans Mar 06 '14 at 21:23
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Phil Mar 06 '14 at 22:33
  • thanks for the help here guys. sorry i didn't respond to these back then. it was pretty inconsiderate. i appreciate the help. – Eswar Dhinakaran Mar 25 '21 at 19:46

1 Answers1

0

I think instead of the while loop you have up there, you'll probably want to do something like this to return all the results as JSON from PHP to the client. Then, you can then loop through the results on the JavaScript side where you'll want to create your LatLng points on the map.

$data = array();
while($foobar = $entry->fetch_assoc()){
    $data[]['longitude'] = $foobar['Longitude'];
    $data[]['latitude'] = $foobar['Latitude'];
}
echo json_encode($data);

Then in the success callback on the JavaScript side, you would want to do something like this:

success: function ( data ) {  
    $.each( data, function( index, point) {
        // create a map point using point.latitude and point.longitude
    });
}
dcarrith
  • 7,510
  • 2
  • 18
  • 9