javascript - How to call controller from ajax and send $_POST -
i want call controller ajax , send post database. after post sent, display database dropdown list.
but don't know do. here code.
ajax script inside view add_produk.php
<script> function getid(val){ // ajax function $.ajax({ type: "post", url: "<?php base_url();?>index.php/getdata", data: "id_kategori="+val, success: function(data){ $("#subcat").html(data); } }); } </script>
i want load controller getdata.php , send $_post controller, controller getdata.php :
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class getdata extends ci_controller { public function index() { if(!empty($_post["id_kategori"])){ $idcat = $_post["id_kategori"]; $sql = "select * sub_kategori id_kategori = $idcat"; $result = $this->db->query($sql); foreach ($result $subcat) { ?> <option value="<?php echo $subcat["id_sub_kategori"];?>"><?php echo $city["nama_sub_kategori"];?></option> <?php } } } } ?>
how can fix problem?
i think wrong in here data: "id_kategori="+val dont know php, u can try "data:{id_kategori:val}" luck
Comments
Post a Comment