If I hard code $x as in $x="a" it works fine. Problem is $x can be a,b or c . Actually, heres the actual code:
<SCRIPT LANGUAGE="JavaScript"><!--
function popSelect(arraySize) {
var tempArray=new Array(arraySize);
//define your temp_array with the lodges that match up
//with the location, as passed from the previous page
for (var j=0;j<arraySize;j++) {
tempArray[j]=document.test_form.resort[document.test_form.resort.selectedIndex].value+'_'+j;
}
for (var i=1;i<=arraySize;i++) {
document.test_form.hotel[i]=new Option(tempArray[i-1]);
//put whatever argument for which lodge is to be selected by
//default after the change, instead of the i==2
if (i==2) {
document.test_form.hotel[i].selected=true;
}
}
}
-->
</script>
<?
//$these arrays should come from your database. You could use SQL queries and while loops
//to define the options for the select objects instead of creating these arrays
$test_array_loc=array("Marmaris","Icmeler","Bodrum");
$test_array_Marmaris=array("Marmaris_1","Marmaris_2","Marmaris_3");
$test_array_Icmeler=array("Icmeler_1","Icmeler_2","Icmeler_3");
$test_array_Bodrum=array("Bodrum_1","Bodrum_2","Bodrum_3");
//this defines a dynamic variable, selecting the proper array based on the location chosen
$x=$resort;
$y=$hotel;
$tempvar="$test_array_".$x;
$temphotel=$$tempvar;
?>
<form name="test_form">
<select onChange="popSelect(<?echo sizeof($$temphotel)?>)" name="resort">
<option><-select an element->
<?
foreach ($test_array_loc as $test_element) {
?>
<option <?if ($test_element==$resort)
{
echo "selected";?>
<?}?>value="<?echo $test_element?>"><?echo $test_element?>
<?
}
?>
</select>
<select name="hotel">
<option><-select an element->
<?
foreach ($$temphotel as $test_element) {
?>
<option <?if ($test_element==$y) {?>selected <?}?>value="<?echo $test_element?>"><?echo $test_element?>
<?
}
?>
I am sure I am wrong but I guess thats wh I am here 🙂