(and sorry ahead of time with this being this type of fundamental question at its core)

I've got a MySQL database with location info, along with a PHP script that may put a brand new, user-posted location in to the database. I wish to then (immediately) discover the distances between your recently-posted location along with a subset from the existing locations within the database utilizing a Google Maps directions request, and store these distances within the database.

I'm able to choose the locations subset via PHP and employ JavaScript to fetch the length data with Google Maps, but how do you then transfer these JavaScript variables to my database? -What's the easiest method to save several JavaScript variables separate records inside a MySQL database?

Interesting help!


If you have the distances correctly loaded into variables inside your JS code, you'll be able to make an XmlHttpRequest to transmit these to your server in which a PHP script will place them in to the database. The pseudo-code could be something similar to this:


function storeDistance(pointId1, pointId2, distance) {
  var xhr = new XmlHttpRequest();
  xhr.open("POST", "/savedistance.php", true);
  xhr.send("pid1=" + pointId1 + "&pid2=" + pointId2 + "&distance=" + distance);


$pointId1 = $_POST['pid1'];
$pointId2 = $_POST['pid2'];
$distance = $_POST['distance'];
mysql_query("insert into distances values ($pointId1, $pointId2, $distance)");

I'm Chinese,Please excuse my poor English。 I've got a project experienced exactly the same problem, If it's between two points A and B You are able to make reference to http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm

If the amount of points

        `editroutes = JSON.parse(editroutes);
        origin = editroutes.ag.origin;
        destination = editroutes.ag.destination;
        var ag = editroutes.ag.waypoints;

            if(typeof ag[i].location == 'object'){
                waypoints.push({location:new google.maps.LatLng(ag[i].location.Ia, ag[i].location.Ja),stopover:ag[i].stopover});
            }else if(typeof ag[i].location == 'string'){

        avoidHighways =  editroutes.ag.avoidHighways;
        avoidTolls = editroutes.ag.avoidTolls;

        var request = {
            origin: origin,
            destination: destination,
            waypoints: waypoints,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            optimizeWaypoints: false,
            avoidHighways: avoidHighways,
            avoidTolls: avoidTolls
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {