PHP How can I keep the selected option from a drop down to stay selected on submit?

前端 未结 2 1091
误落风尘
误落风尘 2021-01-28 23:40

I have:


                        
    
提交评论

  • 2021-01-29 00:10

    First of all, give the option element a value attribute. This makes the code more robust, because it does not break should you decide to alter the text of an option. After that:

    <?php $topic = $_REQUEST['topic']; ?>
    <?php $attr = 'selected="selected"'; ?>
    <select name="topic" style="margin-bottom:3px;"> 
        <option value="1" <?php echo $topic == 1 ? $attr : ''; ?>>General Question</option>
        <option value="2" <?php echo $topic == 2 ? $attr : ''; ?>>Company Information</option>
        <option value="3" <?php echo $topic == 3 ? $attr : ''; ?>>Customer Issue</option>
        <option value="4" <?php echo $topic == 4 ? $attr : ''; ?>>Supplier Issue</option>
        <option value="5" <?php echo $topic == 5 ? $attr : ''; ?>>Request For Quote</option>
        <option value="6" <?php echo $topic == 6 ? $attr : ''; ?>>Other</option>
    </select>
    
    0 讨论(0)
  • 提交回复
    热议问题