javascript - codeigniter csrf not work in select option -
i want make step input select option, in case, make 3 step, when select first option show next option 2, option 3. using ajax , set $config['csrf_protection'] = true; in codeigniter config file. in first select option (#kategori) work , show next value in second select option, in step 3 select option (#sub1 or secon function of javascript) n't work. thank before.
this view:
<?php echo form_open_multipart('',array('class'=>'form-horizontal'));?> <?php echo form_label('kategori','id_kategori',array('class'=>'col-sm-2 control-label'));?> <select id="kategori" name="id_kategori"> <option value=""></option> <?php foreach($kategori $kategori_umum) { echo '<option value='.$kategori_umum->id.'>'.$kategori_umum->nama_kategori.'</option>'; } ?> </select> <select id="sub1" name="id_kategori_sub1"> //step 2 <option value=""></option> </select> <select id="sub2" name="id_kategori_sub2"> //step 3 <option value=""></option> </select>
ajax :
<script type="text/javascript"> $('#kategori').change(function(){ var kategori_id = $('#kategori').val(); //alert(state_id); if (kategori_id != ""){ var post_url = "<?php echo base_url();?>masuk/produk/get_sub1"; $.ajax({ type: "post", url: post_url, data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>','kategori_id':kategori_id}, datatype: 'json', success: function(kategori_sub1, datatype) //calling response json array 'kategori_sub1' { $('#sub1').empty(); $('#sub1').show(); $.each(kategori_sub1,function(id,sub1) { var opt = $('<option />'); // creating new select option each group opt.val(id); opt.text(sub1); $('#sub1').append(opt); $('#sub2').hide(); }); }, error:function(xhr) { alert("terjadi kesalahan"); } }); //end ajax } else { $('#sub1').empty(); $('#sub2').empty(); }}); $('#sub1').mouseout(function(){ var sub1_id = $('#sub1').val(); if (sub1_id != ""){ var post_url = "<?php echo base_url();?>masuk/produk/get_sub2"; $.ajax({ type: "post", url: post_url, data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>','sub1_id':sub1_id}, datatype: 'json', success: function(kategori_sub2, datatype) { $('#sub2').empty(); $('#sub2').show(); $.each(kategori_sub2,function(id,sub2) { var opt = $('<option />'); opt.val(id); opt.text(sub2); $('#sub2').append(opt); }); }, error:function(xhr) { alert("kesalahan"); } }); //end ajax }});</script>
i suspect problem using wrong event mouseout
.
you should using change
same way first 1 works does.
beyond more information needed actual requests inspecting in browser dev tools
Comments
Post a Comment