0

I would like to use PHP variable in Java Script, I would like to get the variables of longitude and latitude which is returned from Select statement read from mysql database on the server.

here with my code below and what I have tried:

    <?php

...


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Latitude, Longitude FROM mytable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $long = " ". $row["Longitude"]. "<br>";
    $lat = " ". $row["Latitude"]. "<br>";
    $latlong = " ". $row["Longitude"]. " , " . $row["Latitude"]. "<br>";
    echo $long;  //echo's -25.1234
    echo $lat; //echo's 25.1234
    echo $latlong; //echo's -25.1234 ,  25.1234
    }
} else {
    echo "0 results";
}
$conn->close();

?> 

<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Tracker</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"></script>
</head> 
<body>

  <div id="map" style="width: 1200px; height: 750px;"></div>

  <script language="JavaScript"> 

            var locations = [
              ['FirstColumnString<br/> SecondColumnString', -25.038326, 25.038326], //I would like to pass Lat Long from PHP code from above
            ];

        // I've tried
        //  ['FirstColumnString<br/>SecondColumnString', <?php echo $long; ?>, <?php echo $lat; ?>,
        //  ['FirstColumnString<br/>SecondColumnString', <?php echo $latlong ?>,
        //  ['FirstColumnString<br/>SecondColumnString', "<?php echo $long; ?>", <?php echo $lat; ?>,

...

  </script>
</body>
</html>
4
  • So you tried, and what are the results? Commented Jul 25, 2015 at 19:42
  • '<?php echo $long; ?>', you need the single quotes :). PHP will simply output it, to JS this could look like a variable Commented Jul 25, 2015 at 19:43
  • you'd need to save the $lats and $longs into an array, then eigther json_encode it to pass it to js, or iterate over it. So the problem is how to get the values from your while-loop to a javascript-readable variable. Commented Jul 25, 2015 at 19:49
  • You need to AJAX the lat/long into the locations Commented Jul 25, 2015 at 19:50

2 Answers 2

2

You should save your locations into an array first:

<?php

    $locations = Array();

    while($row = $result->fetch_assoc()) {
       $loc = Array("lat" => $row["Latitude"], "long" => $row["Longitude"]);
       $locations[] = $loc;
    }
?>

then you can pass it to javascript:

<script>
var locations = <?php echo json_encode($locations) ?>;
// check what you've got:
console.log(locations);
</script>

you could then access your values like this:

var firstLocationLatitude = location[0].lat;

If you need to have your js-array exactly as you've posted change php to:

    $clmString = "FirstColumnString<br/> SecondColumnString";
    while($row = $result->fetch_assoc()) {
       $loc = Array($clmString,$row["Latitude"],$row["Longitude"]);
       $locations[] = $loc;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Try replacing

$long = " ". $row["Longitude"]. "<br>";
$lat = " ". $row["Latitude"]. "<br>";

with

$long = $row["Longitude"];
$lat = $row["Latitude"];

and then

var locations = [
    ['FirstColumnString<br/> SecondColumnString', <?php echo $long; ?>, <?php echo $lat; ?>],
];

3 Comments

this will give him only the very last location!
@Jeff This was to answer to his question on how to echo the variables. Of course, if he has more than one row as result, he needs to use an array or echo the locations (javascript) array from inside a php loop to have all the locations,
@MattCris Yes, technically that's the correct answer. But I assumed the point here is to look and think further, to guide to better overall solutions.

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.