0
<?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
  • 1
    var address = "<?php echo $zip; ?>"; Commented Dec 11, 2013 at 11:54
  • 1
    I dont know how often this question was answered in the one month im registered on SO. Commented Dec 11, 2013 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. Commented Dec 11, 2013 at 12:19

3 Answers 3

2

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.

2
  • Though it's safer to use json_encode to prevent code injection. Commented Dec 11, 2013 at 11:55
  • Not only with array, use it with all variables, as explained in the countless answers to the countless questions of the same kind. Commented Dec 11, 2013 at 11:59
1
var address = "<?php echo $zip ?>";
3
  • I dont think this will work in doublequotes, php interpreter doesnt recognize the php-tags in doublequotes. Commented Dec 11, 2013 at 11:56
  • 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. Commented Dec 11, 2013 at 12:08
  • hm ok, didnt know that, i thought he wont react to the php tags.^^ Commented Dec 11, 2013 at 12:12
0

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

var address = "<?php echo $zipCode' ?>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.