title: How to retain Select option after submitting the form in php
body: <p>I looked at several community forums, and I am unable to figure it out on how to retain select option value after validation fails.</p>
  
  <p>Here is the code that works for me, but values disappear when submit button is submitted.</p>
  
  <pre><code>&lt;select id="service" name="service" class="searchoption"&gt;
               &lt;option value=""&gt;-- Select Service Name --&lt;/option&gt;
               &lt;?php
               $resultservice = mysqli_query($con,"Select * from services") ?&gt;
               &lt;?php
               while ($line = mysqli_fetch_array($resultservice)) {
               ?&gt;
               &lt;option value="&lt;?php echo $line['serviceid'];?&gt;"&gt; &lt;?php echo $line['service'];?&gt; &lt;/option&gt;
  
              &lt;?php
              }
              ?&gt;
              &lt;/select&gt;
  </code></pre>
  
  <p>Here is what I tried and doesn't work for me:</p>
  
  <pre><code>&lt;select id="service" name="service" class="searchoption"&gt;
               &lt;option value=""&gt;-- Select Service Name --&lt;/option&gt;
               &lt;?php
               $resultservice = mysqli_query($con,"Select * from services") ?&gt;
               &lt;?php
               while ($line = mysqli_fetch_array($resultservice)) {
               ?&gt;
               &lt;option value="&lt;?php echo $line['serviceid']; if ($_POST['service'] == $service) {echo 'selected="selected"'} echo $line['serviceid']; ?&gt;"&gt; &lt;?php echo $line['service'];?&gt; &lt;/option&gt;
  
              &lt;?php
              }
              ?&gt;
              &lt;/select&gt;
  </code></pre>