0

i have a dropdown for month and year , but after submit this dropdown alwas back to default (january) . i used 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>

i referenced this code from this question but its not work ,can someone help me ? and its my controler

 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);

}
Adhik Mulat
  • 538
  • 2
  • 10
  • 39

2 Answers2

0

your JS is getElementById but in your dropdown there is no id, update your dropdown as below

<select name="bulan" id="bulan">
...
...
</select>
Karthik Sekar
  • 625
  • 4
  • 7
0

Edit the view_status controller :

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);

}

Edit select bulan script :

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

Edit select tahun script :

<script type="text/javascript">
    document.getElementById('tahun').value = "<?php echo $tahun;?>";
</script>
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • its work but make my table error :( . Trying to get property of non-object – Adhik Mulat Dec 17 '18 at 08:50
  • ok i change it [hasil] and this month its work , but this year is didnt work , its because my php script ? or where i can paste the Js script ? – Adhik Mulat Dec 17 '18 at 08:55
  • I've updated my answer, separating the `month` and `year` data – Hasta Dhana Dec 17 '18 at 08:56
  • It is better to use form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]]) function to generate the Select control and select the default value instead of using JS to select the default value. https://www.codeigniter.com/userguide3/helpers/form_helper.html?highlight=select#form_dropdown – Nish Dec 17 '18 at 12:13
  • @Nish he/she have a static hardcoded array list, doing that would be requiring extra work of assigning data to array first – Hasta Dhana Dec 17 '18 at 12:18