0

code:

<?php

    mysql_connect("localhost", "bikemap", "pedalhard") or die(mysql_error()); 
    mysql_select_db("test") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM gpsdata");     
    $aData = array();
    while($row = mysql_fetch_assoc($data))
        $aData[$row['idgpsdata']] = array($row);
        $json = json_encode($aData);
?>

    var json = "<?php echo $json; ?>";
    document.write(json.length);
    alert('half done');

    for(var x=0; x < json.length; x++) {
        document.write("<li>"+ x + " " + json[x]+ "</li>");
    }

Output of the code:

20

0 <
1 ?
2 p
3 h
4 p
5
6 e
7 c
8 h
9 o
10
11 $
12 j
13 s
14 o
15 n
16 ;
17
18 ?
19 >

if you take out the line numbers is: "<?php echo $json; ?>"

Looks suspiciously like a line where I'm trying to transfer the php variable $json to javascript variable json.

I've tried every type of bracketry and quote to get this to work. Anyone see any mistakes or have any suggestions?

Colin Brock
  • 20,219
  • 9
  • 43
  • 61
Loren Zimmer
  • 467
  • 1
  • 5
  • 26
  • 1
    Are you sure that the PHP code is being executed? That is, are you requesting the page from a web server with PHP enabled or are you just opening the page from the file system in the browser? – Vincent Ramdhanie Nov 16 '11 at 04:41
  • @VincentRamdhanie I am running the code from a site hosted with PHP enabled. – Loren Zimmer Nov 16 '11 at 04:43
  • @LorenZimmer, why don't you "view source" on the output of your script and show us that. – Brad Nov 16 '11 at 04:44
  • http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines – Shomz Nov 16 '11 at 04:44
  • @Brad this kind of development is new to me where would I "view source" from? – Loren Zimmer Nov 16 '11 at 04:51
  • from mozilla: [23:56:39.850] An unbalanced tree was written using document.write() causing data from the network to be reparsed. For more information https://developer.mozilla.org/en/Optimizing_Your_Pages_for_Speculative_Parsing @ http://98.169.0.140:8080/Map/trials/testii.html:5 is this what you were mentioning Brad? – Loren Zimmer Nov 16 '11 at 04:57
  • The code is doing exactly what you programmed it too. What did you expect to see? – James Anderson Nov 16 '11 at 05:05
  • @Shomz I did try it without success thanks for the find though – Loren Zimmer Nov 16 '11 at 05:06
  • @JamesAnderson I thought that it would pass the contents of the variable $json from the php script to var json in javascript. I did not expect it to pass the text of the code to json in javascript. – Loren Zimmer Nov 16 '11 at 05:08
  • Does it matter if this a part of aa javascript file? – Loren Zimmer Nov 17 '11 at 00:48

3 Answers3

1

Try

var json = {<?php echo $json; ?>};

or maybe

var json = eval("{<?php echo $json; ?>}");

Be sure your code is interpreted as php by the server. The file extension '.js' is usually not configured in this way.

If you want to embed your php file as a js, you'd better to rename it as .php and add the required header like that :

<?php
header("Content-type","text/javascript");
?>

var json = {<?php echo $json; ?>};
Naypa
  • 182
  • 6
0

You could try:-

var json = <? echo $json ?>;

You cannot execute php code inside the Javascript. Instead you must get php to write the relevent javascript code;

James Anderson
  • 26,221
  • 7
  • 45
  • 76
0

You could do it other way, that is, put your javascript inside php:

echo "<script>
var json = $json; 
document.write(json.length);
alert('half done');

for(var x=0; x < json.length; x++) {
    document.write(\"<li>\"+ x + \" \" + json[x]+ \"</li>\");
}
</script>";
Arfeen
  • 2,455
  • 2
  • 26
  • 47