如何打印在PHP下拉列表中选择的值或将其设置为选中?

时间:2015-12-29 18:29:51

标签: php html

我想将值设置为已在下拉列表中选择的选定内容,并且还要打印已在该表单中提交的值。

当我打印print_r [$ value]; 我只是得到一些价值观。

以下是我的代码:

      <form method="post" action="" id="date_date" name="date_data">

            <div class="col-sm-4">
              <h3><label for="sel1">Select From Date:</label></h3>
              <h3><?php echo $this->input->post('date_data');?></h3>
              <input type="text" id="datepicker" name="date_data">
            </div>

            <div class="col-sm-4">
              <h3><label for="sel1">Select To Date:</label></h3>
              <h3><?php echo $this->input->post('to_data');?></h3>
              <input type="text" id="datepicker_to" name="to_data">
            </div>
            <div class="col-sm-4">

            <?php
            $user_type = $this->session->userdata('user_type');

            if($user_type =="admin")

            {
            ?>
            <div class="form-group">

                <h3><label for="sel1">Select Employoee Name:</label></h3>
                <select name="attendance-list" id="attendance-list" class="form-control" >




                  <?php foreach($attendance_dropdown as $value)
            { 
           ?>

            <option id="emp" value="<?php echo $value['emp_id'];?>"><?php echo $value['emp_name'];?></option>
            $dropdown = str_replace("value='".$rid."'","value='".$rid."' selected=\"selected\"",$dropdown);
        <?php

          } 
          ?>

                </select>


                <?php echo "<br>"; ?>

            </div>
          <?php  
            }


            else
            {
            ?>
            <div class="form-group">

                <input type="hidden" name="attendance-list" value="<?php echo $this->session->userdata('emp_id'); ?>">

            </div>

           <?php


            }
            ?>
         </div> <!--Col-sm-8-->
         <div class="col-sm-4">
          <h3><label for="sel1"></label></h3>
          <input type="submit" value="submit" class="btn btn-primary">
        </div>

    </form>

1 个答案:

答案 0 :(得分:1)

以下是您的回答,它将选择您上次选择的下拉状态...

如果需要,可以从选项<option id="emp"中删除id,或使用唯一ID。

<select name="attendance-list" id="attendance-list" class="form-control" >
<?php 
$save_drop_down_value = $this->input->post('attendance-list"'); 
 //change this line according to your project..

foreach($attendance_dropdown as $value) { 

  $select = "";
  if( $save_drop_down_value == $value['emp_id']){
    $select = "selected";
  }

?>

<option <?php echo $select; ?> value="<?php echo $value['emp_id'];?>">
    <?php echo $value['emp_name'];?>
</option>

<?php } ?>