Динамические select'ы (AJAX)

ityler

Новичок
Динамические select'ы (AJAX)

Есть отличный код для динамического вывода данных select'ов с без перезагрузки.

Код используется для поиска объектов. Все прекрасно работает, НО хоть убейте не могу понять как сделать, чтобы при показе результатов поиска в select'ы автоматически показывались (selected) выбраные ранее опшены..

Код:
<script language=Javascript>

function Inint_AJAX() {
   try { return new ActiveXObject(\"Msxml2.XMLHTTP\");  } catch(e) {} //IE
   try { return new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert(\"XMLHttpRequest not supported\");
   return null;
};

function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //
               } 
          }
     };
     req.open(\"GET\", \"/js/location/locale_ofc.php?data=\"+src+\"&val=\"+val); //
     req.setRequestHeader(\"Content-Type\", \"application/x-www-form-urlencoded;charset=windows-1251\"); // set Header
     req.send(null); //
}

window.onLoad=dochange('region', -1); 
</script>
Код:
     //
     header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
     header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     header ("Cache-Control: no-cache, must-revalidate");
     header ("Pragma: no-cache");
     header("content-type: application/x-javascript; charset=windows-1251");
     $data=$_GET['data'];
     $val=$_GET['val'];

     //
$dbhost = "localhost";
$dbuser = "xxxxx";
$dbpass = "xxxxx";
$dbname = "xxxxx";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");  
     if ($data=='region') { 
          echo "<select name='region' onChange=\"dochange('district', this.value), dochange('metro', this.value), dochange('name', this.value)\">\n";
          echo "<option value=''>-----Выбрать-----</option>\n";
          $result=mysql_db_query($dbname,"SELECT location_ID, location_name FROM Classificator_location WHERE location_comment LIKE 'reg' order by location_name");
          while(list($id, $name)=mysql_fetch_array($result)){
               echo "<option value=\"$id\">$name</option> \n" ;
			   }
          }
	 
	if ($data=='district') {
          echo "<select name='district'>\n";
          echo "<option value=''>-----Выбрать-----</option>\n";
          $val2=$val;
          $val = substr($val,0,6);                                 
          $result=mysql_db_query($dbname,"SELECT location_ID, location_name FROM Classificator_location WHERE location_ID != '$val2' AND location_ID LIKE '$val%' AND location_comment LIKE 'disreg' ORDER BY location_name");
          while(list($id, $name)=mysql_fetch_array($result)){
               if ($id==$sel2){
               echo "<option value=\"$id\">$name</option> \n" ;
			   }
          }
   
    
	 
	else if ($data=='metro') {
          echo "<select name='metro'>\n";
          echo "<option value=''>-----Выбрать-----</option>\n";
          $val2=$val;
          $val = substr($val,0,6);                                 
          $result=mysql_db_query($dbname,"SELECT location_ID, location_name FROM Classificator_location WHERE location_ID != '$val2' AND location_ID LIKE '$val%' AND location_comment LIKE 'met' ORDER BY location_name");
          while(list($ID, $name)=mysql_fetch_array($result)){       
               echo "<option value=\"$ID\">$name</option> \n" ;
          }
     } 
	  echo "</select>\n";
 
Сверху