473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.php"
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_assoc($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.js to read this markers
array, even if it's a simple alert call like
"alert(markers[3].longitude"...

Oct 2 '06 #1
3 1409
<da************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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.php"
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_assoc($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.js to read this markers
array, even if it's a simple alert call like
"alert(markers[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_data.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
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Oct 2 '06 #2
da************@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.php"
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_assoc($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.js to read this markers
array, even if it's a simple alert call like
"alert(markers[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.
js*******@attglobal.net
==================
Oct 2 '06 #3
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.php"
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_assoc($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.js to read this markers
array, even if it's a simple alert call like
"alert(markers[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.
js*******@attglobal.net
==================
Oct 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.