1
class Map
{
public $Id;
public $longitudes;
public $latitudes;
public $lat_init;
public $lng_init;
public static function createMap($Id){
    global $latitude, $longitude;
    $dbh = Database::connect();
    $query = "SELECT * FROM `cartes` WHERE Id=? ";
    $sth = $dbh->prepare($query);
    $sth->setFetchMode(PDO::FETCH_CLASS, 'Map');
    $sth->execute(array($Id));
    if ($sth->rowCount() === 0) return NULL;
    $map=$sth->fetch();
    $sth->closeCursor();
    $lat=$map->latitudes;
    $lng=$map->longitudes;
    $latitude=unserialize($lat);
    var_dump($latitude);
    $longitude=unserialize($lng);
    echo'<script type="text/javascript">'
    ,'var lat =  json_encode($latitude); 
      var lng =  json_encode($longitude);   
      draw(lat,lng);
        '

    ,'</script>';

}  
}
<?php 
$dbh=Database::connect();
Map::createMap(6);
?>

When i excute this code, the following error appears: "$latitude is not defined". var_dump($latitude) is ok. I think the script doesn't recognize $latitude but i don't know why. Any help? thanks

1
  • 1
    Hello! Don't forget to accept an answer, you even get 2 rep for it! Commented Apr 4, 2014 at 21:13

3 Answers 3

6

If you're encoding it into JSON, you'll need to wrap it in quotes correctly (you can't call functions inside strings):

echo '<script type="text/javascript">
        var lat = '. json_encode($latitude). '; 
        var lng = '. json_encode($longitude). ';   
        draw(lat,lng);
      </script>';
Sign up to request clarification or add additional context in comments.

4 Comments

JSON is (a subset of) Javascript literal syntax, so you don't need to use JSON.parse -- just substitute the result directly into the JS.
It returns the string, but when you echo it into the web page, the quotes are not there.
You could also use $.parseJSON() as a jQuery alternative for better support.
@KyleNeedham I don't post jQuery answers to questions that don't have a jQuery tag.
4

With PHP, single quote are for string literals and will use the name of the variable instead of its value. If you want interpolation (inserting a variable's value into the string), you need to use double quotes.

echo '<script type="text/javascript">',
    "var lat = ". json_encode($latitude)  .";
     var lng = ". json_encode($longitude) .";   
     draw(lat,lng);",
'</script>';

Update: I somehow overlooked the part about json_encode being a PHP call... so the interpolation issue is a moot point, since you just need to concatenate the function's output into the string.

1 Comment

You need to make sure to concatenate the output from PHP function json_encode to the JavaScript string.
1

Functions aren't called inside quoted strings. You need to concatenate it:

echo '<script type="text/javascript">
      var lat =  ' . json_encode($latitude) . ';
      var lng =  ' . json_encode($longitude) . '; 
      draw(lat,lng);
     ';

Comments

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.