How do I set the selected item in a drop down box

后端 未结 11 776
误落风尘
误落风尘 2020-11-29 04:16

Is there any way to set the selected item in a drop down box using the following \'type\' code?


                        
    
提交评论

  • 2020-11-29 04:35

    This is the solution that I came up with...

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">   
        <select name="select_month">
            <?php
                if (isset($_POST['select_month'])) {
                    if($_POST["select_month"] == "January"){
                        echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
                    }
                    elseif($_POST["select_month"] == "February"){
                        echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
                    }
                }
                else{
                    echo '<option value="January">January</option><option value="February">February</option>';
                }
            ?>
        </select>
        <input name="submit_button" type="submit" value="Search Month">
    </form>
    
    0 讨论(0)
  • 2020-11-29 04:36

    My suggestion is to leverage the hidden/collapse attribute. Try with this example:

    <select>
        <option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
    

    in case of null for $row[month] the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...

    0 讨论(0)
  • 2020-11-29 04:39

    Simple way

    <select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
    
    <option value=""><?php echo "Select"; ?></option>
    
    <?php  foreach ($dropdowndetails as $dropdowndetails) { ?>
        <option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
    <?php } ?>
    </select>
    
    0 讨论(0)
  • 2020-11-29 04:39

    A Simple Solution: It work's for me

    <div class="form-group">
        <label for="mcategory">Select Category</label>
        <select class="form-control" id="mcategory" name="mcategory" required>
            <option value="">Please select category</option>
            <?php foreach ($result_cat as $result): ?>
            <option value="<?php echo $result['name'];?>"<?php 
               if($result['name']==$mcategory){
                   echo 'selected';
               } ?>><?php echo $result['name']; ?></option>
                                            }
            <?php endforeach; ?>
        </select>
    </div>
    
    0 讨论(0)
  • 2020-11-29 04:40

    Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.

    function selectdCheck($value1,$value2)
       {
         if ($value1 == $value2) 
         {
          echo 'selected="selected"';
         } else 
         {
           echo '';
         }
         return;
       }
    

    and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns

    <option <?php selectdCheck($row[month],january); ?> value="january">january</option>
    

    :) I hope this function help others

    0 讨论(0)
  • 提交回复
    热议问题