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?

share|improve this question

52% accept rate
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 4:41
@VincentRamdhanie I am running the code from a site hosted with PHP enabled. – Loren Zimmer Nov 16 '11 at 4:43
@LorenZimmer, why don't you "view source" on the output of your script and show us that. – Brad Nov 16 '11 at 4:44
@Brad this kind of development is new to me where would I "view source" from? – Loren Zimmer Nov 16 '11 at 4:51
show 5 more comments
feedback

3 Answers

up vote 1 down vote accepted

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; ?>};
share|improve this answer
I needed just the php file to pass the variables – Loren Zimmer Nov 17 '11 at 6:04
feedback

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;

share|improve this answer
same outcome as above...thanks though – Loren Zimmer Nov 16 '11 at 5:18
feedback

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>";
share|improve this answer
There is no need for this. – Brad Nov 16 '11 at 14:11
This didn't work either – Loren Zimmer Nov 16 '11 at 20:02
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.