4

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?

3
  • can you show how you are using name[i] to get the data? Commented May 3, 2013 at 6:13
  • possible duplicate of Convert php array to Javascript Commented May 3, 2013 at 6:14
  • I used console.log(name[i]) to display this in console Commented May 3, 2013 at 6:17

1 Answer 1

5

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

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

4
  • 1
    It is not "reserved", but rather it points to window.name, an already existing property of the global object (window). Commented May 3, 2013 at 6:15
  • My bad :) so you're saying this.name might work under a class? Commented May 3, 2013 at 6:15
  • Exactly. =] Anywhere that is not in the global scope. Try on console (function(){ var name=1; alert(name); }());. Commented May 3, 2013 at 6:16
  • Thank you so much! After changing it to names it works now! :) Commented May 3, 2013 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.