Dropdown list:How to display selected value in the same dropdown list?

i have 3 dropdown lists:

  1. disease classifications.
  2. disease categories.
  3. disease parent.

values in the categories depend on classifications while values in parent depend on categories. values in classifications populated from an array while for categories are coming from database. i have a problem at 2nd dropdown list. suppose whenever user has selected a value, the form will be submitted using onChange=“this.form.submit()” and the selected value supposely can be viewed in the 2nd dropdown list and values in 3rd dropdown list will appear. but, the truth is, the selected value gone after made selection. how to solve that?

disease_block.php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

include("../ecis_cfg.php");
global $ecisDisease, $ecisDiseaseCategory;
$dcid = isset($_GET['dcid']) ? $_GET['dcid'] : ' ';
//echo "dcid: " . $dcid;
$discatid = isset($_GET['discatid']) ? $_GET['discatid'] : ' ';
//echo "<br>discatid: " . $discatid;
?>
<style type="text/css">
@import url(../css/style_registration.css);
.style3 {font-size: 13px}
body,td,th {
	font-size: 13px;
}
</style>
<form name="form" method="post" action="../process/disease.php?op=register">
  <table width="464" border="0">
    <tr>
      <td colspan="2" id="tblheader">Disease Profile </td>
    </tr>
    <tr>
      <td width="114">Classification</td>
      <td width="334">
        <select name="dis_class" id="dis_class" onChange="this.form.submit();">
          <option value="" selected>Select</option>
          <?php
	  	       foreach($ecisDiseaseCategory->loadDiseaseClassfication() as $key => $value){
	      ?>
          <?php if($key == $dcid){ ?>
          <option value="<?php echo $key; ?>" selected>[<?php echo $key; ?>] <?php echo $value; ?></option>
          <?php } else { ?>
          <option value="<?php echo $key; ?>">[<?php echo $key; ?>] <?php echo $value; ?></option>
          <?php } } ?>
        </select>
</td>
    </tr>
    <tr>
      <td>Category</td>
      <td>
	  <select name="dis_cat" id="dis_cat" onChange="this.form.submit();">
		<option selected>Select</option>
		<?php  $ecisDiseaseCategory->loadDiseaseCategory($dcid); ?>
      </select>
	  </td>
    </tr>
    <tr>
      <td>Name</td>
      <td><input name="dis_name" type="text" id="dis_name"></td>
    </tr>
    <tr>
      <td>Description</td>
      <td><textarea name="dis_desc" id="dis_desc"></textarea></td>
    </tr>
    <tr>
      <td>Parent</td>
      <td>
	  <select name="dis_parent" id="dis_parent">
	  <option selected>Select</option>
	  <option value="0">None</option>
	  <?php $ecisDisease->loadDiseaseParent($discatid); ?>
      </select>
	  </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input type="submit" name="Submit" value="Submit"></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
 

disease.php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

include("../ecis_cfg.php");
global $ecisDisease;

# 1st dropdown list:
# 1. contains classification of diseases
if(!isset($_POST['Submit'])){
	if(isset($_POST['dis_class']) && ($_POST['dis_class'] != "Select") && (isset($_GET['op']) == "register")){
		header("Location: ../templates/disease_block.php?dcid=".$_POST['dis_class']);
	}
}

# 2nd dropdown list:
# 1. data for this 2nd dropdown list depends on selected classification of diseases (from 1st dropdown list)
# 2. select any value from this 2nd dropdown list, will populate data into 3rd dropdown list
if(!isset($_POST['Submit'])){
	if(isset($_POST['dis_cat']) && ($_POST['dis_cat'] != "Select") && (isset($_GET['op']) == "register")){
		header("Location: ../templates/disease_block.php?dcid=".$_POST['dis_class']."&discatid=".$_POST['dis_cat']);
	}
}

# if submit button is clicked
if(isset($_POST['Submit']) && isset($_GET['op']) == "register"){
	if(empty($_POST['dis_class']) || empty($_POST['dis_cat']) || empty($_POST['dis_name']) || $_POST['dis_parent'] == "Select"){
		echo "<strong>Warning:</strong> Please fill in all mandaratory fields.";
		echo "<br><br>";
		echo "<a href=\\"../templates/disease_block.php\\">Back</a>";
	}
	else{
		if($ecisDisease->checkDisease($_POST['dis_cat'], $_POST['dis_name']) == false){
			echo "<strong>Warning:</strong> <strong><u>" . $_POST['dis_name'] . "</u></strong> already in the records.";	
			echo "<br><br>";
			echo "<a href=\\"../templates/disease_block.php\\">Back</a>";
		}
		else
			$ecisDisease->addDisease($_POST['dis_cat'], $_POST['dis_name'], $_POST['dis_desc'], $_POST['dis_parent']);
	}
}

?>

functions involved

#1st dropdown list
function loadDiseaseClassfication()
	{
		$diseaseClass = array("I" => "Certain infectious & parasitic diseases",
							  "II" => "Neoplasms",
							  "III" => "Blood & blood-forming organs",
							  "IV" => "Endocrine, nutritional & metabolic diseases",
							  "V" => "Mental & behavioural disorders",
							  "VI" => "Nervous system",
							  "VII" => "Eye & adnexa",
							  "IX" => "Ear & mastoid process",
							  "X" => "Respiratory system",
							  "XI" => "Digestive system",
							  "XII" => "Skin & subcutaneous tissue",
							  "XIII" => "Musculoskeletal system & connective tissue",
							  "XIV" => "Genitourinary system",
							  "XV" => "Pregnancy, childbirth & the puerperium",
					   		  "XVI" => "Certain conditions originating in the perinatal period",
					          "XVII" => "Congenital malformations, deformations & chromosomal abnormalities"
					         );
		return $diseaseClass;
	}

#2nd dropdown list
function loadDiseaseCategory($classId)
	{
		
		global $ecisDB, $parent;
		$parent = 0;
		$sql = "SELECT *
				FROM DiseaseCategory
				WHERE diseaseCategoryClass = '$classId'
				AND diseaseCategoryParent = '$parent'
				ORDER BY diseaseCategoryName";
		$query = $ecisDB->query($sql) or die(mysql_error());
		$count = $ecisDB->sql_count($query);
		
		if($count <> 0){			
			while($result = $ecisDB->fetch_array($query)){
				echo '<option value="'.$result["diseaseCategoryId"].'">'.$result["diseaseCategoryName"].'</option>';
			}			
		}
	}

#3rd dropdown list
function loadDiseaseParent($discatid)
	{
		global $ecisDB, $parent;
		$parent = 0;
		$sql = "SELECT * FROM Disease WHERE diseaseCategoryId = '$discatid' AND diseaseParent = '$parent'";
		$query = $ecisDB->query($sql) or die(mysql_error());
		$count = $ecisDB->sql_count($query);
		
		if($count <> 0){			
			while($result = $ecisDB->fetch_array($query)){
				echo '<option value="'.$result["diseaseCategoryId"].'">'.$result["diseaseName"].'</option>';
			}			
		}
	}
 <option value="" selected>Select</option> 

in the first block of code. will this not mean that Select is always the selected, no matter what you say after? Maybe put something like

IF (!isset($_POST['discatid'])) {
echo ("selected");
}

Hope I’m not barking up the wrong tree here
Alex

thank you for your reply. “Select” is default at all dropdown lists. just to be displayed only. for example, is user selected “A” at 2nd dropdown list, the form will be submitted to disease.php together with the “A” using onChange=“this.form.submit()”. it will goes to this line of codes.

# 2nd dropdown list:
# 1. data for this 2nd dropdown list depends on selected classification of diseases (from 1st dropdown list)
# 2. select any value from this 2nd dropdown list, will populate data into 3rd dropdown list
if(!isset($_POST['Submit'])){
	if(isset($_POST['dis_cat']) && ($_POST['dis_cat'] != "Select") && (isset($_GET['op']) == "register")){
		header("Location: ../templates/disease_block.php?dcid=".$_POST['dis_class']."&discatid=".$_POST['dis_cat']);
	}
}

then i re-send both values from 1st and 2nd dropdown lists back to previous page using header(). selected value for 1st dropdown list is showed as selected in the 1st dropdown list, and suppose the “A” from 2nd dropdown list also is showed as selected in the 2nd dropdown list, but it never happened. it always go back to “Select”.

this is among my solution but i do not want to do like this. i wanted the process of sql being done in a function which is located in the class file.

<select name="dis_cat" id="dis_cat" onChange="this.form.submit();">
		<option selected>Select</option>
		<?php
			
			global $ecisDB, $parent;
			$parent = 0;
			$sql = "SELECT diseaseCategoryId, diseaseCategoryName
			       FROM DiseaseCategory
				   WHERE diseaseCategoryClass = '$dcid'
				   AND diseaseCategoryParent = '$parent'
				   ORDER BY diseaseCategoryName";
			$query = $ecisDB->query($sql) or die(mysql_error());
			while($result = $ecisDB->fetch_array($query)){
				if($result['diseaseCategoryId'] == $discatid){
			
		?>
		<option value="<?php echo $result['diseaseCategoryId']; ?>" selected><?php echo $result['diseaseCategoryName']; ?></option>
		<?php } else { ?>
		<option value="<?php echo $result['diseaseCategoryId']; ?>"><?php echo $result['diseaseCategoryName']; ?></option>
		<?php } } ?>
      </select>

wflick,

Manuel Lemos has an excellent forms generation and validation class over at phpclasses.org.

it allows for linked selects and i use one that has 3 linked selects - just like your example.

he’s even added an ajax feature so when one select is chosen, a query is sent to the db and the result is displayed in the linked select.

i don’t use that method yet. i populate an array with all possible values and then let the class work through the linking.

i highly recommend the class and i think the license is BSD (free for commercial use).