title: How to show selected option in select tag of html?
body: <p>I am using a select tag from html to show some list. Now when I select one of the option from select tag, I want to load the form again and by the selected option I am running another query and showing the values of another query in another select tag.</p>
  
  <p>Its working properly after selecting any option I am posting a type from which I am getting another data in another select tag, but when I click suppose option TSgt and the form reloads with the TSgt type data, but the option selected in select tag shows as MSgt which is not the one I selected. </p>
  
  <p>If I don't check if the $type is set in _POST array and just show the select tag without an select value like follow :</p>
  
  <pre><code>&lt;option value="1"&gt;SSgt&lt;/option&gt; 
  &lt;option value="2"&gt;TSgt&lt;/option&gt; 
  &lt;option value="3"&gt;MSgt&lt;/option&gt; 
  </code></pre>
  
  <p>Then, default selected value shows SSgt,I want to show the value in select tag which user has selected.</p>
  
  <pre><code>       &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;MCQ Questions&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
  session_start();
  
  $dbh = new PDO('mysql:host=174.75.54;dbname=handbook', 'airman', 'airman'); 
  
      $type = $_POST['type'];
      $question = $_SESSION["question"];
      $optionA = $_SESSION["opt1"];
      $optionB = $_SESSION["opt2"];
      $optionC = $_SESSION["opt3"];
      $optionD = $_SESSION["opt4"];
      $ans = $_SESSION["ans"];
      $chapter = $_SESSION["chapter"];
  ?&gt;
  
  &lt;form method="post" action="mcq.php" enctype="multipart/form-data"&gt;
  &lt;p&gt; Enter the question :&lt;/p&gt; &lt;input name="question" type="text"&gt; &lt;br&gt;&lt;br&gt;
  Select question type : 
  
  &lt;select name="type" id="type" onchange="this.form.submit()"&gt; 
  
  &lt;?php if(isset($_POST['type']))
  { ?&gt;
  &lt;option value="1"  selected=&lt;?=($type==1?"checked":"");?&gt;&gt;SSgt&lt;/option&gt; 
  
  &lt;option value="2"  selected=&lt;?=($type==2?"checked":"");?&gt;&gt;TSgt&lt;/option&gt; 
  &lt;option value="3" selected=&lt;?=($type==3?"checked":"");?&gt;&gt;MSgt&lt;/option&gt; 
  &lt;/select&gt;
  &lt;?php
  }
      else 
      {
      ?&gt;
      &lt;option value="1"&gt;SSgt&lt;/option&gt; 
  &lt;option value="2"&gt;TSgt&lt;/option&gt; 
  &lt;option value="3"&gt;MSgt&lt;/option&gt; 
  &lt;/select&gt;
      &lt;?php
      }
      ?&gt;
  
  &lt;br&gt;&lt;br&gt;
  &lt;?php if(isset($optionA))
  { ?&gt;
  &lt;p&gt; Enter options :&lt;/p&gt;
  Enter option A : &lt;input name="opt1" type="text" value = "&lt;?php echo $optionA?&gt;"&lt;/input&gt; &lt;br&gt;&lt;br&gt;
  
  &lt;?php
  }
  else{
  ?&gt;
  &lt;p&gt; Enter options :&lt;/p&gt;
  Enter option A : &lt;input name="opt1" type="text"&gt; &lt;br&gt;&lt;br&gt;
  &lt;?php   
  }
  ?&gt;
  
  Enter option B : &lt;input name="opt2" type="text"&gt; &lt;br&gt;&lt;br&gt;
  Enter option C : &lt;input name="opt3" type="text"&gt; &lt;br&gt;&lt;br&gt;
  Enter option D : &lt;input name="opt4" type="text"&gt; &lt;br&gt;&lt;br&gt;
  
  Select correct answer :
  
  &lt;select name="ans" id="type"&gt; 
  
  &lt;option value="A"&gt;A&lt;/option&gt; 
  &lt;option value="B"&gt;B&lt;/option&gt; 
  &lt;option value="C"&gt;C&lt;/option&gt; 
  &lt;option value="D"&gt;D&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;br&gt;&lt;br&gt;
  
  Select Chapter :
  
  
  &lt;select name="chapters" id="chapters"&gt; 
  
  &lt;?php 
  
  if(isset($_POST['type']))
  {
      $stmt = $dbh-&gt;prepare("SELECT * FROM chapters where type = :type"); 
      $stmt-&gt;bindParam("type", $type);
  $stmt-&gt;execute(); 
  $results = $stmt-&gt;fetchall(PDO::FETCH_ASSOC); 
  
  if(count($results &gt; 0)){ 
  foreach($results as $row):?&gt; 
  &lt;option value="&lt;?php echo $row['id'];?&gt;"&gt;&lt;?php echo $row['title'];?&gt;&lt;/option&gt; 
  &lt;?php 
  endforeach; 
  }else{?&gt; 
  
  &lt;option value="0"&gt;No data found&lt;/option&gt; 
  &lt;?php 
  } 
  
  }
  
  else{
  
  $stmt = $dbh-&gt;prepare("SELECT * FROM chapters where type = 1"); 
  
  $stmt-&gt;execute(); 
  $results = $stmt-&gt;fetchall(PDO::FETCH_ASSOC); 
  
  if(count($results &gt; 0)){ 
  foreach($results as $row):?&gt; 
  &lt;option value="&lt;?php echo $row['id'];?&gt;"&gt;&lt;?php echo $row['title'];?&gt;&lt;/option&gt; 
  &lt;?php 
  endforeach; 
  }else{?&gt; 
  
  &lt;option value="0"&gt;No data found&lt;/option&gt; 
  &lt;?php 
  } 
  }
  ?&gt; 
  
  &lt;/select&gt; &lt;br&gt;&lt;br&gt;
  &lt;input type="submit" value = "Submit"&gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  &lt;?php
  
  // escape post variables
  $question = $_POST['question'];
  $option1 = $_POST['opt1'];
  $option2 = $_POST['opt2'];
  $option3 = $_POST['opt3'];
  $option4 = $_POST['opt4'];
  $ans = $_POST['ans'];
  $chapter = $_POST['chapters']; 
  
  
  if(!empty($ans) and !empty($question) and !empty($option1) and !empty($option2) and !empty($option3) and !empty($option4) and !empty($type) and !empty($chapter))
  {
      $stmt = $dbh-&gt;prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
  $stmt-&gt;execute(array($question, $option1, $option2, $option3, $option4, $ans,$type,$chapter));
  
  if ($dbh-&gt;lastInsertId())
  {   
      echo 'Question submitted.';
  
      echo '&lt;a href="mcq.php"&gt;Upload another question.&lt;/a&gt;';
      session_destroy();
  }
  else
  {
      echo 'Question could not submit.';
  }
  
  }
  
  else{
  
      $_SESSION["question"] = $question;
      $_SESSION["chapter"] = $chapter;
      $_SESSION["ans"] = $ans;
      $_SESSION["opt1"] = $option1;
      $_SESSION["opt2"] = $option2;
      $_SESSION["opt3"] = $option3;
      $_SESSION["opt4"] = $option4;
  
      echo 'Fill all fields.';
  }
  
  ?&gt;
  </code></pre>
  
  <p>I dont know what is going wrong here. Can anyone help me out please?</p>
  
  <p>Thank you.</p>