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