I have a table being pulled from MySql through PHP.. The TD in the tables have unique IDs and get autoincremented in a loop.
$i=0;
while($row = mysql_fetch_array($result))
{ $i = $i+1;
echo "<tr>";
echo "<td>" . $row['DRAINNAME'] . "</td>";
echo "<td id='lat" . $i ."'>" . $row['LATITUDE'] . "</td>";
echo "<td id='lon" . $i ."'>" . $row['LONGITUDE'] . "</td>";
echo "<td> <input type='button' onclick='detect_load();' value='Click Me' /><br>
<div id='divsec". $i ."' style='width: 200px; height: 200px;'></div> </td>";
echo "</tr>";
}
echo "</table>";
Im writing a Javascript function which should get values from the unique IDs of the TDs..
function getLocation(pos) {
latitude = document.getElementById('lat'+ '<?php echo $i; ?>').innerHTML;
longitude = document.getElementById('lon'+ '<?php echo $i; ?>').innerHTML;
load_map();
}
But the '<?php echo $i; ?>'
dosent seem to work.. :(
I mean, for example--- If the value of i is 2, then td becomes lat2 and lon2
but in javascript, its not taking those ID values i.e. lat2 and lon2..
its giving an error saying its NULL ..
Here is the full Code if you cannot understand it....
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var latitude = '';
var longitude = '';
function getLocation(pos) {
latitude = document.getElementById('lat'+ <?php echo $i; ?>).innerHTML;
longitude = document.getElementById('lon'+ <?php echo $i; ?>).innerHTML;
load_map();
}
function unknownLocation(){alert('Could not find location');}
function detect_load(){
navigator.geolocation.getCurrentPosition(getLocation, unknownLocation);
}
function load_map() {
if(latitude == '' || longitude == '')
return false;
var latlng = new google.maps.LatLng(latitude, longitude);
var config = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('divsec'+ <?php echo $i; ?>),config);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Your requested location!"
});
}
</script>
</head>
<body>
<?php
include 'datalogin.php';
$result = mysql_query("SELECT DRAINNAME, LATITUDE, LONGITUDE FROM draininfo");
echo "<table border='1' name='tab1'>
<tr>
<th>Drain Name</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Show On Map</th>
</tr>";
$i=0;
while($row = mysql_fetch_array($result))
{ $i = $i+1;
echo "<tr>";
echo "<td>" . $row['DRAINNAME'] . "</td>";
echo "<td id='lat" . $i ."'>" . $row['LATITUDE'] . "</td>";
echo "<td id='lon" . $i ."'>" . $row['LONGITUDE'] . "</td>";
echo "<td> <input type='button' onclick='detect_load();' value='Click Me' /><br>
<div id='divsec". $i ."' style='width: 200px; height: 200px;'></div> </td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>