php - Dependent Dropdown and Input not getting populated (Codeigniter, Ajax, jQuery) -
i try cascading dependent select box , input in codeigniter ajax. first step works quite well. securities can loaded, when selecting account. problem starts second step. so, when try after security-selection set appropriate changeable inputs, security-select getting blocked , nothing works. don't understand problem is. please me, nasty blockage dissolve. thanks.
ajax:
$(document).ready(function(){ var _form = $("#trans_form").serializearray(); $('#amount_section').hide(); $('#quantity_section').hide(); $('#accountdrop').on('change',function(){ $("#securitydrop > option").remove(); var accountid = $(this).val(); if(accountid == '#') {return false;} $.ajax({ data: _form, type: "post", url: global_base_url + "custody/get_securities_dropdown/" + accountid, success: function(securities) { $.each(securities,function(id,value) { var opt = $('<option />'); opt.val(id); opt.text(value); $('#securitydrop').append(opt); }); } }); }); $('#securitydrop').on('change',function(){ $('#amount_section').hide(); $('#quantity_section').hide(); var securityid = $(this).val(); if(securityid == '#') {return false;} $.ajax({ data: _form, type: "post", url: global_base_url + "custody/get_security_unit_ajax/" + securityid, success: function(securunit) { if (securunit == "unit") { $('#quantity_section').show(300); }; else if (securunit == "famt") { $('#amount_section').show(300); }; } }); });
});
controller:
public function get_securities_dropdown($account_id){ $securities = $this->custody_model->get_security_by_account($account_id); header('content-type: application/x-json; charset=utf-8'); echo json_encode($securities); } public function get_security_unit_ajax($security_id){ $securunit = $this->custody_model->get_security_unit($security_id); header('content-type: application/x-json; charset=utf-8'); echo json_encode($securunit); }
model:
public function get_accounts_dropdown(){ $accounts = $this->db->select("id id, account_desc descr") ->order_by("descr", "asc") ->get($this->table2)->result(); $accounts_arr; $accounts_arr['#'] = '-- please select account --'; foreach ($accounts $account) { $accounts_arr[$account->id] = $account->descr; } return $accounts_arr; } public function get_security_by_account($account_id){ if(!is_null($account_id)){ $securities = $this->db->where("a.id_account", $account_id) ->select("b.id id, b.security_desc descr") ->join($this->table5 . " b", "b.id = a.id_security") ->order_by("descr", "asc") ->get($this->table6 . " a"); if($securities->num_rows() > 0){ $securities_arr; foreach ($securities->result() $security) { $securities_arr[$security->id] = $security->descr; } return $securities_arr; } } return; }
view:
<?php echo form_open_multipart(site_url("custody/add_transaction_pro"), array("id" => "trans_form")) ?> <div> <label for="accountdrop">account</label> <div> <?php echo form_dropdown('accountdrop', $account_arr, '#', 'id="accountdrop"'); ?> </div> </div> <div id="security_section"> <label for="security_select">security</label> <div> <select name="securitydrop" class="required" id="securitydrop"> <option value="#">-- please select security --</option> </select> </div> </div> <div id="quantity_section"> <label for="quantity">quantity</label> <div id="quantityinput"> <input type="text" id="quantity" name="quantity"> </div> </div> <div id="amount_section"> <label for="settl_amount">amount</label> <div id="amountinput"> <input type="text" id="settl_amount" name="settl_amount"> </div> </div>
i have solved it. error quite simple, unnecessary semicolons in ajax part. syntax if() { } else if{ }
Comments
Post a Comment