Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<?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>
share|improve this question
1  
var address = "<?php echo $zip; ?>"; –  Amal Murali Dec 11 '13 at 11:54
1  
I dont know how often this question was answered in the one month im registered on SO. –  Y U NO WORK Dec 11 '13 at 11:55
 
pd: since finally you are getting a javascript array i think u need to iterate using a loop to get all the zip code. –  Mr coder Dec 11 '13 at 12:19
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
 
Though it's safer to use json_encode to prevent code injection. –  Marcel Korpel Dec 11 '13 at 11:55
 
+1 for that markel :) –  Mr coder Dec 11 '13 at 11:56
 
Not only with array, use it with all variables, as explained in the countless answers to the countless questions of the same kind. –  Marcel Korpel Dec 11 '13 at 11:59
 
@MarcelKorpel updated :) –  Mr coder Dec 11 '13 at 12:00
add comment
var address = "<?php echo $zip ?>";
share|improve this answer
 
I dont think this will work in doublequotes, php interpreter doesnt recognize the php-tags in doublequotes. –  Y U NO WORK Dec 11 '13 at 11:56
 
But this worked for me. –  user2936213 Dec 11 '13 at 12:03
1  
@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. –  Mr coder Dec 11 '13 at 12:08
 
hm ok, didnt know that, i thought he wont react to the php tags.^^ –  Y U NO WORK Dec 11 '13 at 12:12
add comment

Simply echo it and store it in a JavaScript variable. Here's how:

var address = "<?php echo $zipCode' ?>";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.