'How to display old value in dropdown on edit form then keep the selected value when the form submit failed?

The dropdown's option values were shown from a table on the database. When I clicked "Edit Form," the old values were successfully shown. For example, the option that was previously selected is 'A.' Then I want to change the option to 'B.' When I click the save button, I let the submit fail on purpose, so the page redirected back to the form. Still, the dropdown select value is back to 'A.' I want it to remain 'B' after the submit failed. How can I fix this?

View

 <select name="kategori_kunjungan_id" class="form-control">
      <?php foreach ($kategorikunjungan as $kk) { ?>
           <option value="<?= $kk['id'] ?>" <?= ($kk['id'] == $kunjungan['kategori_kunjungan_id']) ? 'selected' : ''; ?>><?= $kk['name'] ?></option>
      <?php } ?>
 </select>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source