I have found a javascript function that gets the name of a country when it is clicked on a map and a php snippet which gets the name of a country randomly from a file. What I want to do is to compare the result between these two so that I can tell if the user clcked on the map the right country according to the one appearing above in the question. I am not sure how to do it since they are not exactly simple variables and I tried to assign the php variable to a javascript one with no luck. Any guidance would be appreciated.
....
function getCountry(latLng) {
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
for(var i = 0; i < results[0].address_components.length; i++) {
if(results[0].address_components[i].types[0] == "country") {
alert(results[0].address_components[i].long_name);
}
}
}
else {
alert("No results");
}
}
else {
alert("Status: " + status);
}
}
);
}
....
Can you locate <?php
$countries=file("countries.txt");
$number_countries=count($countries);
if($number_countries!==0){$number_countries.=-1;};
$random=rand(0,$number_countries);
$countryy=$countries[$random];
echo "$countryy";
?> on the map?
This is a bad practice, you should use ajax, but here is what you want.
Can you locate <?php
$countries=file("countries.txt");
$number_countries=count($countries);
if($number_countries!==0){$number_countries.=-1;};
$random=rand(0,$number_countries);
$countryy=$countries[$random];
echo "$countryy";
?> on the map?
<script type="text/javascript">
var country = <?php echo json_encode($countryy); ?>
function getCountry(latLng) {
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
for(var i = 0; i < results[0].address_components.length; i++) {
if(results[0].address_components[i].types[0] == "country") {
if(results[0].address_components[i].long_name == country) {
alert("right");
} else {
alert("wrong");
}
}
}
}
else {
alert("No results");
}
}
else {
alert("Status: " + status);
}
}
);
}
</script>