title: Retain Data Recieved from Database on HTML form
body: <pre><code>&lt;?php
  include_once 'database_connect.php';
  $conn = new dbconnection();
  $dbcon = $conn-&gt;connect();
  if (!$dbcon) {
      die("Fail".mysqli_error($dbcon));
  }
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form name="frm" method="post" action='&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;'&gt;
      &lt;table width="50%" border="1" cellpadding="3" cellspacing="3" align="center"&gt;
          &lt;?php
          $value1 = array();
          $select_query = "SELECT Distinct branch FROM subjects";
          $result = mysqli_query($dbcon, $select_query);
          if (!$result) {
              die("Fail".mysqli_error($dbcon));
          }
  
          while ($row = mysqli_fetch_array($result)) {
              $value1[] = $row['branch'];
          }
  
          ?&gt;
          &lt;tr&gt;
              &lt;td&gt;Branch
              &lt;td&gt;&lt;select name="branch" id="branch" onchange="document.frm.submit();"&gt;
                      &lt;option&gt;Select Branch&lt;/option&gt;
                      &lt;?php
                      foreach ($value1 as $gets)
                          echo "&lt;option value={$gets}&gt;{$gets}&lt;/option&gt;";
                      ?&gt;
                  &lt;/select&gt;
  
                  &lt;?php
                  $value2 = array();
                  if (isset($_POST['branch'])) {
                      $branch = $_POST['branch'];
                      $getsub_query = "SELECT sub_code FROM subjects where branch='$branch'";
                      $result2      = mysqli_query($dbcon, $getsub_query);
                      if (!$result2) {
                          die("Fail\n".mysqli_error($dbcon));
                      }
                      while ($row1 = mysqli_fetch_array($result2)) {
                          $value2[] = $row1['sub_code'];
                      }
                  }
                  ?&gt;
          &lt;tr&gt;
              &lt;td&gt;Subject Code
              &lt;td&gt;&lt;select name="subcode" id="subcode"&gt;
                      &lt;option&gt;Subject Code&lt;/option&gt;
                      &lt;?php
                      foreach ($value2 as $gets)
                          echo "&lt;option value={$gets}&gt;{$gets}&lt;/option&gt;";
                      ?&gt;
                  &lt;/select&gt;
  </code></pre>
  
  <p>This code gets the first drop down list branch from data base. When we select value from it, second drop down list get filled from database . but problem is when i select option in first drop down list, the selected option does not remain their in first drop down list . but second drop down list fills correctly. i want that option i have selected should remain selected. Like its state should be changed. I think first drop down list gets filled again on form load.</p>