Javascript function can't read variables set by php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dan.cao.nguyen@gmail.com

    Javascript function can't read variables set by php

    OK, I've basically copied this code ad verbatim from a book to
    test...what I have is an index.html page that refers to "map_data.p hp"
    and "map_functions. js"...

    map_data.php draws some coordinates from a database and outputs them
    into an array called markers. However, when I load up the index.html
    page, it says "markers" is undefined...

    Here's the code in map_data.php:

    var markers = [
    <?php while($row = mysql_fetch_ass oc($result)): ?>
    <?= $joiner ?>
    {
    'latitude': <?= $row['latitude'] ?>,
    'longitude': <?= $row['longitude'] ?>,
    'lawyer': <?= $row['lawyer']?>,
    'city': <?= $row['city']?>,
    'business_name' : '<?= addslashes($row['business_name'])?>',
    'address': '<?= addslashes($row['address'])?>'
    }
    <?
    $joiner = ',';
    ?>
    <?php endwhile; ?>
    ];

    I can't get anything in the map_functions.j s to read this markers
    array, even if it's a simple alert call like
    "alert(mark ers[3].longitude"...

  • Kimmo Laine

    #2
    Re: Javascript function can't read variables set by php

    <dan.cao.nguyen @gmail.comwrote in message
    news:1159762503 .749397.173870@ i42g2000cwa.goo glegroups.com.. .
    OK, I've basically copied this code ad verbatim from a book to
    test...what I have is an index.html page that refers to "map_data.p hp"
    and "map_functions. js"...
    >
    map_data.php draws some coordinates from a database and outputs them
    into an array called markers. However, when I load up the index.html
    page, it says "markers" is undefined...
    >
    Here's the code in map_data.php:
    >
    var markers = [
    <?php while($row = mysql_fetch_ass oc($result)): ?>
    <?= $joiner ?>
    {
    'latitude': <?= $row['latitude'] ?>,
    'longitude': <?= $row['longitude'] ?>,
    'lawyer': <?= $row['lawyer']?>,
    'city': <?= $row['city']?>,
    'business_name' : '<?= addslashes($row['business_name'])?>',
    'address': '<?= addslashes($row['address'])?>'
    }
    <?
    $joiner = ',';
    ?>
    <?php endwhile; ?>
    ];
    >
    I can't get anything in the map_functions.j s to read this markers
    array, even if it's a simple alert call like
    "alert(mark ers[3].longitude"...

    Tried viewing the source? It would be very helpful if you could show what
    php actually outputs there. Since markers is undefined, are you sure you've
    got the map_data.php inside a javascript tag? As in:

    <script type="text/javascript">
    <?php require(map_dat a.php ?>
    </script>



    And I just gotta say... You're actually using while(): endwhile! Burn the
    witch! Vade retro satana! (Nothing wrong with the syntax though, it IS valid
    php, but man, it's just unorthodox!)

    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • Jerry Stuckle

      #3
      Re: Javascript function can't read variables set by php

      dan.cao.nguyen@ gmail.com wrote:
      OK, I've basically copied this code ad verbatim from a book to
      test...what I have is an index.html page that refers to "map_data.p hp"
      and "map_functions. js"...
      >
      map_data.php draws some coordinates from a database and outputs them
      into an array called markers. However, when I load up the index.html
      page, it says "markers" is undefined...
      >
      Here's the code in map_data.php:
      >
      var markers = [
      <?php while($row = mysql_fetch_ass oc($result)): ?>
      <?= $joiner ?>
      {
      'latitude': <?= $row['latitude'] ?>,
      'longitude': <?= $row['longitude'] ?>,
      'lawyer': <?= $row['lawyer']?>,
      'city': <?= $row['city']?>,
      'business_name' : '<?= addslashes($row['business_name'])?>',
      'address': '<?= addslashes($row['address'])?>'
      }
      <?
      $joiner = ',';
      ?>
      <?php endwhile; ?>
      ];
      >
      I can't get anything in the map_functions.j s to read this markers
      array, even if it's a simple alert call like
      "alert(mark ers[3].longitude"...
      >
      I suspect you have short tags disabled in your php.ini file.

      Instead of '<?= ' you should use '<?php echo '.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Dan Bee

        #4
        Re: Javascript function can't read variables set by php

        Thanks all,

        The idiotic error I made is that in the outputted array, I did not put
        quotation marks around the strings. Doh.



        Jerry Stuckle wrote:
        dan... wrote:
        OK, I've basically copied this code ad verbatim from a book to
        test...what I have is an index.html page that refers to "map_data.p hp"
        and "map_functions. js"...

        map_data.php draws some coordinates from a database and outputs them
        into an array called markers. However, when I load up the index.html
        page, it says "markers" is undefined...

        Here's the code in map_data.php:

        var markers = [
        <?php while($row = mysql_fetch_ass oc($result)): ?>
        <?= $joiner ?>
        {
        'latitude': <?= $row['latitude'] ?>,
        'longitude': <?= $row['longitude'] ?>,
        'lawyer': <?= $row['lawyer']?>,
        'city': <?= $row['city']?>,
        'business_name' : '<?= addslashes($row['business_name'])?>',
        'address': '<?= addslashes($row['address'])?>'
        }
        <?
        $joiner = ',';
        ?>
        <?php endwhile; ?>
        ];

        I can't get anything in the map_functions.j s to read this markers
        array, even if it's a simple alert call like
        "alert(mark ers[3].longitude"...
        >
        I suspect you have short tags disabled in your php.ini file.
        >
        Instead of '<?= ' you should use '<?php echo '.
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...