OK. What I want to achieve is this...
-- The user makes a selection from a select menu
-- Two textareas are populated with the relevant data from a database based on the above selection.
1/ Get Data from Database
$query = "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
"FROM investment_type";
$result = mssql_query($query) or die('Select Error');
2/ Declare Arrays
$type_array = array();
$type1_array = array();
$id_array = array();
3/ Loop Through Query Results + Assign Values to Arrays
while($row = mssql_fetch_array($result))
{
// option string
$option .= '<option value="'.$row['investment_type_ID'].'">'.$row['investment_type'].'</option>';
// arrays to build javascript array string
$type1_array[] = $row['inv_note2'];
$type_array[] = $row['inv_note'];
$id_array[] = $row['investment_type_ID'];
}// end while
4/ Create string for javascript assoc array format. "key1:value1", "key2:value2",
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
}
5/ Function when select menu changed
<script language="Javascript">
// Define first array
var notes = new Array();
// Assign PHP value to Javascript array
var notes = '<?php print_r($javascript_array) ?>';
// Define second Array
var notes2 = new Array();
var notes2 = '<?php print_r($javascript_array1) ?>';
// Put all elements into a string
document.write(notes.join(","));
document.write(notes2.join(","));
function change(id)
{
document.getElementById('inv_type_note').innerHTML = notes[id];
document.getElementById('inv_type_note2').innerHTML = notes2[id];
}
</script>
6/ Select Menu
<select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
<option value="0">Select Investment Type</option>
<?php
echo $option;
?>
</select>
Make sense? Is what I am doing correct?
Thanks.