duanji6997 2017-12-09 10:26
浏览 43
已采纳

使用codeigniter选择显示选项

I want to ask for my code option selected with 2 tabel in page register

table prodi
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id_prodi    | varchar(3)  | NO   | PRI | NULL    |       |
| nama_prodi  | varchar(30) | NO   |     | NULL    |       |
| id_fakultas | varchar(3)  | NO   | MUL | NULL    |       |
| id          | int(2)      | NO   | MUL | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

and

table jurusan
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| id_fakultas   | varchar(3)  | NO   | PRI | NULL    |       |
| nama_fakultas | varchar(40) | NO   |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+

here's view :

 <label for="fakultas">Fakultas : </label>
  <select id="fakultas" name="fakultas_user"  onChange="fakultas">

    <?php 
      foreach ($qfakultas as $fakultas) {
        echo "<option value=".$fakultas->kd_fakultas.">".$fakultas->nama_fakultas."</option>";    
      }
    ?>    
  </select>

  <hr>

  <label for="jurusan">Jurusan  : </label>
  <select id="jurusan" name="jurusan_user" onChange="jurusan">
    <?php 
      foreach ($qprodi as $prodi) {
        echo "<option value=".$prodi->kd_prodi.">".$prodi->nama_prodi."</option>";    
      }
    ?>  
  </select>

but I will display 'nama_prodi' based on 'id_fakultas"

register with option value

How to display when I click 'nama_fakultas' then display 'nama_prodi' based on 'id_fakultas' ?

Thanks

  • 写回答

1条回答 默认 最新

  • douyuan1049 2017-12-09 13:29
    关注

    Here is the Ajax code that I can help, only to get nama_prodi based on id_falkutas.

    You can use this code for your project, there is something you need to do.
    Not just copy the code. Just a few step
    (ID Lang : Jangan dicopy mentah2, hanya beberapa langkah saja untuk edit).

    1. Change YOUR_URL_HERE into your base_url() or create var for it.
    2. Change nama_controller into your controller name in CI_Controller
    3. Change nama_fungsi into your function to get the data from your model (database).
      Example : ambil_data_jurusan.
    4. Remove your foreach - echo <option> and onchange from your view.
    5. Add the script into your view files or create a new .js file.
    6. Please help me to mark as correct answer if this code helped you. ( tolong dibantu gan )

    See Code Example :

    <script>
    $("#falkutas").on('change', function() {
       $.ajax({
            type:"POST",
            url:'YOUR_URL_HERE/nama_controller/nama_fungsi',
            data:{"falkutas_user":falkutas_user,"csrf_token":$("input[name=csrf_token]").val()},
            dataType:'json',
            success: function(result) {
                for(var i=0; i<result.length; i++) {
                    res += '<option value="'+result[i]['id_prodi']+'">'+result[i]['nama_prodi']+'</option>';
                }
                $(document).ready(function() {
                     $("#prodi").html(res);
                });
            },
            error: function(result) {
                $(document).ready(function() {
                    $("#prodi").html(''); //empty select value
                });
            }
        });
    }
    </script>
    

    Then, in your function code :

    // Inside function ambil_data_jurusan(), harusnya sudah jalan kalau gini
    $return = $this->model->get_jurusan($this->input->post('falkutas',TRUE));
    if(count($return)>0) {
        echo json_encode($return);
    }
    

    In your view, just let select empty like this.

    <select id="jurusan" name="jurusan_user">
    
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?