<?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>
3 Answers 3
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.
answered Dec 11, 2013 at 11:53
R R
2,9562 gold badges27 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Marcel Korpel
Though it's safer to use
json_encode to prevent code injection.Marcel Korpel
Not only with array, use it with all variables, as explained in the countless answers to the countless questions of the same kind.
var address = "<?php echo $zip ?>";
answered Dec 11, 2013 at 11:55
user2936213
1,0111 gold badge8 silver badges19 bronze badges
3 Comments
Realitätsverlust
I dont think this will work in doublequotes, php interpreter doesnt recognize the php-tags in doublequotes.
R R
@YUNOWORK it will work ,the server starts executing the php scripts when it first encounters the<?php tag so i dont think double quotes will make any difference.
Realitätsverlust
hm ok, didnt know that, i thought he wont react to the php tags.^^
Simply echo it and store it in a JavaScript variable. Here's how:
var address = "<?php echo $zipCode' ?>";
answered Dec 11, 2013 at 11:56
Basit
1,8402 gold badges31 silver badges50 bronze badges
Comments
default
var address = "<?php echo $zip; ?>";