I have the following code in javascript
I am storing a php array in a javascript array below
<script type="text/javascript">
var name=new Array();
<?php
for($i=0;$i<count($ids); $i++)
{
echo "name[$i]='".$name[$i]."';";
}
?>
</script>
When i alert name[1], in firefox I am able to see the value of name[1], but when i run the code in chrome i get undefined in the alert box.
How can i solve this. Do I have to change the way I am storing the array or do I have to do something else.
Because you're committing a great sin in declaring your variable:
name
, which is a reserved keyword in chrome, just open console and type for (n in window){if (n === 'name'){console.log(true)}}
, it'll log truenew Array();
, use []
on php front: either use the double quotes consistently, or use single quotes (but that's just my personal preference):
echo "name[$i]='{$name[$i]}';";
echo 'name['.$i.']="'.$name[$i].'";';
Like the way you place the curly's, though... :-P