I can’t figure out the the code for – How to retain the values from dropdown category lists after wrong form submission? Any suggesstions, thanx in advance.

<select class="selectpicker btn-block" name="coupon_cat" id="category" >                                                    
        <option selected value=""> Категори Сонгох</option>
        <?php                                                       
             $args['exclude'] = "12,20"; // omit these two categories
             $categories = get_categories($args);
             foreach($categories as $category) { ?>
             <option value="<?php echo $category->term_id;?>"><?php echo $category->name;?>
        </option>
    <?php } ?</select>

2 Answers
2

Like Ash was saying, something like this using $_REQUEST or $_POST will do it.

<?php
  $args['exclude'] = "12,20"; // omit these two categories
  $categories = get_categories($args);
?>
<select class="selectpicker btn-block" name="coupon_cat" id="category">
  <option value="">Категори Сонгох</option> <!-- if nothing else is selected, this will be selected automatically -->
  <?php foreach($categories as $category): ?>
    <?php $selected = (isset($_REQUEST['coupon_cat']) && $_REQUEST['coupon_cat'] == $category->term_id)?'selected="selected"':''; ?>
    <option value="<?php echo $category->term_id;?>" <?php echo $selected ?>><?php echo $category->name; ?></option>
  <?php endforeach; ?>
</select>

Leave a Reply

Your email address will not be published. Required fields are marked *