1

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>
0

You can check with $_REQUEST['coupon_cat'], Which gives value of selected category then compare it with your loop value so, you can make it selected.

$_REQUEST['coupon_cat'] will help you.

  • thanx for the suggestion, plz bear with me, can you plz tell me where I put this code in the above code. – anandmongol Jun 3 '16 at 4:11
  • thanx @ash, it just works fine!! – anandmongol Jun 3 '16 at 4:39
0

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>
  • thanx @paul, it just works fine!! – anandmongol Jun 3 '16 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.