I need to pass a js variable to a php variable. I know this can't be done directly and I have been trying to do it with ajax but I just can't figure it out.
<form name="menu">
<div>
<select name="list1" size="1" onchange="setOptions(document.menu.list1.options
[document.menu.list1.selectedIndex].value,document.menu.list2,document.menu.list3);">
<option value=" " selected></option>
<option value="one">item1</option>
<option value="two">item2</option>
<option value="three">item3</option>
<option value="four">item4</option>
</select><br><br>
<select name="list2" size="1"onchange="setOptions(document.menu.list2.options
[document.menu.list2.selectedIndex].value,document.menu.list3,' ');">
<option value=" " selected>Select an option</option>
</select><br><br>
<select name="list3" size="1">
<option value=" " selected>Select an option</option>
</select><br>
</div>
</form>
<script>
function setOptions(chosen, selbox, selbox2)
{
selbox.options.length = 0;
if (chosen == " ")
{
selbox.options[selbox.options.length] = new Option('Select an option',' ');
selbox2.options[selbox2.options.length] = new Option("Select an option"," ");
setTimeout(setOptions(' ',document.menu.list3),5);
}
if (chosen == "one")
{
showList2(selbox, chosen);
}
if (chosen == "two")
{
showList2(selbox, chosen);
}
if (chosen == "three")
{
showList2(selbox, chosen);
}
if (chosen == "four")
{
showList2(selbox, chosen);
}
//some code snipped
}
function showList2(selbox, chosen)
{
<?php
$n = 0;
$list1 = chosen; //where i need to pass the variable
foreach($list[$list1] as $key => $value)
{
$key_array[$n] = $key;
$Lname[$n] = $list[$list1][$key]['name'];
$n++;
}
$jsArray = json_encode($key_array);
echo "var jsKeys = ". $jsArray . ";";
$jsArray = json_encode($Lname);
echo "var jsNames = ". $jsArray . ";";
?>
for (var i=0;i<jsKeys.length;i++)
{
var result = selbox2.options[selbox2.options.length] = new Option(jsNames[i],jsKeys[i]);
if(i < jsKeys.length-1)
{
var result = setTimeout(setOptions(jsKeys[0],document.menu.list3,' '),5);
}
}
return(result);
}
</script>