I am working on a demo webpage which put some products' volumes onto Google Map in some geojson polygons.
The code that to put volume onto Google Map:
map.data.addListener('click', function(event){
//Get area-information from polygon
var area_information = event.feature.getProperty('area');
/*var product_volume = showVolume(area_information); DOES NOT WORK
instead, I am sending the result to a div called p_volume and assign the value of p_volume to product_volume*/
var infowWindow_contentString = '<b>Area: </b>' + area_information+ '<br>' + '<b>Volume: </b>' + product_volume;
polygon_infoWindow.setContent(infowWindow_contentString);
polygon_infoWindow.open(map);
polygon_infoWindow.setPosition(event.latLng);
});
Now the showVolume() function:
function showVolume(area_information){
var xhr;
var result;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE 8 and older
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var areainfo = "areainfo=" + area_information;
xhr.open("POST", "get-data.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(areainfo);
xhr.onreadystatechange = display_data;
//Display Data
function display_data() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
//alert(xhr.responseText);
document.getElementbyId('p_volume').textContent = xhr.responseText;
}
else {
alert('There was a problem with the request.');
}
}
}
};
Which calls the get-data.php.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db_name = "mockdata";
$areainfo = $_POST['areainfo'];
$con = mysqli_connect($servername, $username, $password, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$stmt = $con->prepare('SELECT Volume from mockdata WHERE Area = ?');
$stmt->bind_param('s', $areainfo);
$stmt->execute();
$result = $stmt->get_result();
//echo $areainfo; //Testing if parameter passed into php
while ($row = $result->fetch_assoc()) {
// do something with $row
echo "{$row["Volume"]}";
}
mysqli_close($con);
?>
For each query, I am expecting only one integer from the Volume in my table. I checked the parameter is passed to get-data.php by simply
echo $areainfo;
I learned:
var product_volume = showVolume(area_information);
does not work....
Edit
It seems that the
<div><id = p_volume></div>
is getting the query result. However, my product_volume
still couldn't get the result from the p_volume
by simply doing
product_volume = getElementbyId('p_volume').innerHTML;
I am wondering what is wrong.
I am new to all these 3 languages. Please be gentle when criticizing "Go learn xxx language".
Thanks in advance.