Ajax on_change will not insert on foreach

2 selects. {Select1} chooses a year. There is an onchange for select 1 that makes a file path ($mydir) from {select1}. Then it does the following:

$files1 = scandir($mydir);
foreach ($files1 as $value) {
$sql=“insert into filelist(filename) values (’”.$value."’)";
sc_exec_sql($sql);
}

{select2} has to do its select from the filelist table, but NOTHING happens. The table filelist has nothing. When I take the exact code and put it in a button and click the button after choosing {select1} it works perfectly. Thoughts?