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've tried

var name = <?php echo json_encode($eventname); ?>; 

and

var name = new Array("<?php echo implode('","', $eventName);?>"); 

to parse my name string array from PHP to Javascript. It displayed as

var name = ["lalalala","Lalala","test"]; 

and

var name = new Array("lalalala","Lalala","test"); 

in viewsource, but when I tried to use name[i] to get the string, it returned the character, not the string. The size of the array name is also not 3, but 20 (which is the total number of characters plus three ","). How can I fix this?

share|improve this question
    
can you show how you are using name[i] to get the data? –  Yogesh Suthar May 3 '13 at 6:13
    
possible duplicate of Convert php array to Javascript –  Rikesh May 3 '13 at 6:14
    
I used console.log(name[i]) to display this in console –  abenoy05 May 3 '13 at 6:17
add comment

1 Answer 1

up vote 5 down vote accepted

Pretty sure because 'name' points to window.name (Thanks Fabrício Matté). Look Here

It works fine if you change 'name' to 'names'.

share|improve this answer
1  
It is not "reserved", but rather it points to window.name, an already existing property of the global object (window). –  Fabrício Matté May 3 '13 at 6:15
    
My bad :) so you're saying this.name might work under a class? –  Dave Chen May 3 '13 at 6:15
    
Exactly. =] Anywhere that is not in the global scope. Try on console (function(){ var name=1; alert(name); }());. –  Fabrício Matté May 3 '13 at 6:16
    
Thank you so much! After changing it to names it works now! :) –  abenoy05 May 3 '13 at 6:21
add comment

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.