Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to insert php variables into javascript.

Here is the broken code:

 var tokens = [<?
 $result = mysql_query("SELECT * FROM accounts ORDER BY id DESC") or die (mysql_error()); 
 while ($row = mysql_fetch_array($result)) { 
 $user_name = $row['user'];
 $user_id = $row['id'];
 echo "['$user_name','$user_id'],";
 }
 ?>]
share|improve this question
1  
And which is the question? Any error? –  Nathan Campos Nov 2 '09 at 0:09
    
I've attempted a quick clarification of the question. Hopefully Gully will confirm that this is what he's trying to do. –  Peter Boughton Nov 2 '09 at 0:39
    
Duplicate of stackoverflow.com/questions/1663750/… –  Kevin Peno Nov 2 '09 at 23:36

3 Answers 3

up vote 3 down vote accepted

Use PHP's json_encode & then echo out to javascript directly, e.g:

$fruit = array("banana", "apple", "strawberry", "raspberry", "orange");
$json_fruit = json_encode($fruit);

echo "var fruit = $json_fruit;";

Edit: I have updated this answer to no longer use eval() since it is not required. I had just started using JSON when I first answered this question.

share|improve this answer
3  
eval isn't necessary unless youa re trying to pass it back as a response via ajax (in that case it should go on the js side anyway). json_encode spits out a valid js object/value. –  Kevin Peno Nov 3 '09 at 0:00
1  
-1 For the unneeded eval. This shows you don't understand what JSON is. –  Alin Purcaru May 13 '11 at 14:32
    
Thanks for the reminder on this one. I have updated the answer so it no longer uses eval(). –  Klinky May 14 '11 at 13:49

You do this:

 echo "['$user_name','$user_id'],";

... meaning that it will generate this:

[ ['a','b'], ['a','b'], ['a','b'], ['a','b'], ]

Notice that it will always end with a "," and this is an incorrect syntax.

Correct syntax is :

[ ['a','b'], ['a','b'], ['a','b'], ['a','b'] ]

I'm not a PHP guy, but I'd say something like this would fix it :

 var tokens = [<?
 $result = mysql_query("SELECT * FROM accounts ORDER BY id DESC") or die (mysql_error()); 
 $i = 0;
 while ($row = mysql_fetch_array($result)) { 
 $i++;
 $user_name = $row['user'];
 $user_id = $row['id'];
 echo "['$user_name','$user_id']";
 if($i < sizeof($result)-1 ) // incorrect syntax, but you get the point
   echo ","
 }
 ?>]
share|improve this answer
    
Ah, I think I missed that the question is about serializing PHP variables to JavaScript. There should be a built-in PHP function that does that (but I'm not a PHP guy either, so don't know what it might be). –  Peter Boughton Nov 2 '09 at 0:35
    
Yeah I made the same error at first when reading the question ^^ True there must be a function doing this somewhere, but this should do the trick as well. –  marcgg Nov 2 '09 at 0:40
    
Just remembered that there's an implode function that converts an array to a delimited list - coping with the trailing comma issue - but that requires an array to work with, which would probably be more work to create than your existing counter method anyway. –  Peter Boughton Nov 2 '09 at 0:45

PHP runs on the server. JavaScript runs on the client.

You can generate JavaScript from PHP, but you cannot run PHP in JavaScript.

(It is worthwhile reading up on how the various components of the web work, specifically HTTP, to understand all this better.)

If you want to perform PHP actions in response to client actions in the browser without reloading the whole page, investigate 'ajax'.

share|improve this answer

Your Answer

 
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.