Reputation: 15
<?php
$query = mysql_query("SELECT products_zipcode FROM products ")or die(mysql_error());
while($row = mysql_fetch_array($query))
{
$zip = $row['products_zipcode'];
}
?>
here how to assign $zip variable value to java script variable var address = zip;
<script type="text/javascript">
function codeAddress(zip) {
var address = zip;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
</script >
Here changed the code as per the answers
<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("coachup_db1") or die(mysql_error());
?>
<?php
$query = mysql_query("SELECT products_zipcode FROM products ")or die(mysql_error());
while($row = mysql_fetch_array($query))
{
$zip[] = $row['products_zipcode'];
// echo("codeAddress($zip)");
}
?>
<div id="map-canvas"></div>
<style>
#map-canvas {
width: 300px;
height: 200px;
margin: 0px;
padding: 0px;
border: 0px; padding: 0px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 4,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {
var address = <?php echo json_encode($zip); ?>;
var overallcontent = <?php echo json_encode($zip); ?>;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, 'load', codeAddress);
</script>
Upvotes: 0
Views: 577
Reputation: 2956
you can assign a php variable to a javascript variable using the following method.
var address =<?php echo json_encode($zip) ?>;
on the other hand if you want to assign a php array to a javascript variable than:
var overallcontent = <?php echo json_encode($type); ?>;
where $type
is a php array.
Upvotes: 2
Reputation: 1840
Simply echo
it and store it in a JavaScript variable. Here's how:
var address = "<?php echo $zipCode' ?>";
Upvotes: 0