Hey everyone! I'm having an issue with reading PHP variables from Javascript which should be loaded on the client-side already. What I have done is:
1) Populate a PHP array on load from data in a mySQL database.
2) Populate a HTML form combo box with elements.
Basically, each element in the combo box, when clicked, populates a SECOND combo box with elements taken from the PHP array. So like:
Blue -> Sky, Water, Blueberries.
Green -> Grass, Grapes
(this is an example lol, not what I'm actually doing)
The first combo box has Blue and Green. And Sky, Water, Blueberries, Grass and Grapes are stored in the mySQL database. The PHP code reads them from the database and sorts them out, placing them in the array with each index holding the elements belonging to a different colour (in the form of "Sky|Water|Blueberries" and "Grass|Grapes").
The elements in the first combo box all have the value of the ID that the MySQL database uses to identify which secondary items belong to it (say Blue is 2, and Green is 5). So effectively I then have an array like this:
Code:
$colors[2] = "Sky|Water|Blueberries"
$colors[5] = "Grass|Grapes"
Now, in Javascript, I read the value of the currently selected item in the first combo box through an onChange event:
Code:
var id = document.colors.options[document.colors.selectedIndex].value;
And then attempt to read the appropriate string from the PHP variable through:
Code:
var colorNames = "<?php echo ($colors["+id+"]) ?>";
Except it doesn't seem to like my inserting that Javascript variable 'id' in there. The ID variable does indeed return the selected value, and you can read from the $colors if you hardcode an index in there e.g.:
Code:
var colorNames = "<?php echo ($colors[2]) ?>";
But not dynamically using the ID variable. Is it actually possible to do this? Have I done something stupid elsewhere? Am I missing the plot completely? Thanks for any input you can provide. :)