jackson77
jackson77

Reputation: 11

Google Maps API results variables into php

I want to export search result variables (latitude and longtitude) from JS as the php variables. I know that it's necessary to have another php file, for example test.php.

variables below:

        marker.setPlace({
          placeId: place.place_id,
          location: results[0].geometry.location,

        });
        marker.setVisible(true);

        //infowindowContent.children['place-name'].textContent = place.name;
        //infowindowContent.children['place-id'].textContent = place.place_id;
        infowindowContent.children['place-address'].textContent =
            //results[0].formatted_address;
            results[0].geometry.location.lat(), 
            results[0].geometry.location.lng(),
            infowindow.open(map, marker); 
        });
    });
}

Upvotes: 0

Views: 49

Answers (1)

SphynxTech
SphynxTech

Reputation: 1849

You can simply use XMLHttpRequest with ajax and JSON (for js and php) in order to pass data to your php file:

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      console.log (this.responseText);
    }
  };
  xhttp.open("GET", "demo_get.asp?marker=" + JSON.stringify (marker) + "&infoWindow=" + JSON.stringify (infowindow), true);
  xhttp.send();

Then, in php:

$marker = json_decode ($_GET["marker"]);
$infoWindow = json_decode ($_GET["infoWindow"]);
// Do stuff...

Tell me if you have some questions.

Upvotes: 1

Related Questions