如何打印选定的下拉值?

时间:2019-04-12 17:36:49

标签: php html5 forms html-form

我们有一个报价回复表,我们想向客户显示该服务将在最短和最长时间内完成。为此,我们进行了两次下拉。

  1. 最小持续时间
  2. 最长持续时间。

同时发送两个选定值时,如何在答案中显示它?

  <form method="post" class="quotation-reply default-hidden" id="editquotationreply">
  <div class="clearfix row input_fields_wrap">
    <div class="col-md-6">
        <div class="form-group">

if($time_duration = 15){
                $timeduration = array(15 => esc_html__('15 Mins', 'service-finder'),30 => esc_html__('30 Mins', 'service-finder'),45 => esc_html__('45 Mins', 'service-finder'),60 => esc_html__('1 Hr', 'service-finder'),75 => esc_html__('1 Hr 15 Mins', 'service-finder'),90 => esc_html__('1 Hr 30 Mins', 'service-finder'),105 => esc_html__('1 Hr 45 Mins', 'service-finder'),120 => esc_html__('2 Hrs', 'service-finder'),150 => esc_html__('2 Hrs 30 Mins', 'service-finder'),180 => esc_html__('3 Hrs', 'service-finder'),210 => esc_html__('3 Hr 30 Mins', 'service-finder'),240 => esc_html__('4 Hrs', 'service-finder'));
            }
                ?>

<select class="sf-select-box form-control sf-form-control" name="min_timeduration" data-live-search="true" title="<?php esc_html_e('Time Duration', 'service-finder'); ?>">
                <option value=""><?php esc_html_e('Max Time Duration', 'service-finder'); ?></option>
                <?php
                if(!empty($timeduration)){
                    foreach($timeduration as $key => $value){
                        echo '<option value="'.esc_attr($key).'">'.esc_html($value).'</option>';    
                    }
                }
                ?>
              </select>  </div>
            </div>

1 个答案:

答案 0 :(得分:0)

提交表单后,您的页面是否已重新加载,之后所选的值应显示为选中状态?在这种情况下,您可以使用PHP的$_SESSION变量来存储选定的$key,并在foreach循环中可以检查该变量并将“ selected”添加到相应的选项中。

session_start();
if(!empty($_POST['min_timeduration'])) {
    $_SESSION['selected']  = $_POST['min_timeduration'];
}
$timeduration = [...];
foreach($timeduration as $key => $value) {
    echo '<option value="' . esc_attr($key) .'"';
    if(!empty($_SESSION['selected']) && $_SESSION['selected'] == $key) {
        echo ' selected ';
    }
    echo '>'. esc_html($value) .'</option>';    
}