I would like to change the options in a select element based on what i select in another select element (dropdown box).
I have two select boxes (RUNS and SAMPLES) on my PHP page. What i intend to do is to change options in the second select box based on a query that is performed against a database using the option selected in first select box.
I have a table in my database that connects "SAMPLES" to "RUNS". The first select box is populated by a set of 'RUNS' on page load. I would like the 'SAMPLES' that belong to the selected RUN to populate in the SAMPLES box.
The HTML would be something like
<select name='run_id' id='run_id>
<option value='1'>RUN 1</option>
<option value='2'>RUN 2</option>
<option value='4'>RUN 3</option>
<option value='5'>RUN 4</option>
</select>
The backend table "sample_runs" has columns run_id and sample_name. I have a PHP script as follows,
<?php
mysql_connect("localhost","uname","pwwd");
mysql_select_db("dbname");
$run_id = $_GET['run_id'];
$sql = "select distinct sample_name from samples where run_id = '$run_id'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)){
echo '<option value='.$rs['sample_name'],'>'.$rs['sample_name']."</option>";
}
?>
I can modify the PHP script (easy part), but can someone share some code with me on how to populate the sample name options based on the run_id selected in the RUNS box.
Greatly appreciate the help.