提交后,所选下拉菜单始终会更改

时间:2018-12-17 07:37:49

标签: codeigniter dropdown selected

我有一个月份和年份的下拉列表,但是提交后,该下拉列表始终恢复为默认值(一月)。我使用了Codeigniter

<label>Bulan</label><br>
                  <select name="bulan">
                      <!-- <option value="">Pilih</option> -->

                      <option value="01">Januari</option>
                      <option value="02">Februari</option>
                      <option value="03">Maret</option>
                      <option value="04">April</option>
                      <option value="05">Mei</option>
                      <option value="06">Juni</option>
                      <option value="07">Juli</option>
                      <option value="08">Agustus</option>
                      <option value="09">September</option>
                      <option value="10">Oktober</option>
                      <option value="11">November</option>
                      <option value="12">Desember</option>
                  </select>
                        <script type="text/javascript">
                          document.getElementById('bulan').value = "<?php echo $_GET['bulan'];?>";
                        </script>
                </div>
                <div class="tahun" align="center">
                <?php
                $now=date('Y');
                echo "<select name=tahun>";
                for ($a=2010;$a<=$now;$a++)
                {
                     echo "<option value='$a'>$a</option>";
                }
                echo "</select>";
                ?>
                <script type="text/javascript">
                  document.getElementById('tahun').value = "<?php echo $_GET['tahun'];?>";
                </script>
                <br><br>
                <input type="submit" name="submit">
                </div>
              </form>

我从此question引用了此代码  但是它不起作用,有人可以帮我吗? 和它的控制者

 public function view_status(){


    $year = $this->input->post('tahun');
    $month = $this->input->post('bulan');

    $this->data['hasil']=$this->app_model->status_data($year,$month);

    $this->load->view('view_status',$this->data);

}

2 个答案:

答案 0 :(得分:0)

您的JS是getElementById,但是在您的下拉列表中没有id,请按如下所示更新您的下拉列表

<select name="bulan" id="bulan">
...
...
</select>

答案 1 :(得分:0)

编辑view_status控制器:

public function view_status(){


    $year = $this->input->post('tahun');
    $month = $this->input->post('bulan');

    $this->data['hasil'] = $this->app_model->status_data($year,$month);
    $this->data['bulan'] = $month;
    $this->data['tahun'] = $year;

    $this->load->view('view_status',$this->data);

}

编辑选择bulan脚本:

<script type="text/javascript">
    document.getElementById('bulan').value = "<?php echo $bulan;?>";
</script>

编辑选择tahun脚本:

<script type="text/javascript">
    document.getElementById('tahun').value = "<?php echo $tahun;?>";
</script>