codeigniter csrf在select选项中不起作用

时间:2022-10-06 16:14:04

I want to make step input with select option, in this case, i make 3 step, when select first option then will show next option 2, then option 3. i am using ajax and i set $config['csrf_protection'] = TRUE; in Codeigniter config file. In first select option (#kategori) is work and show next value in second select option, but in step 3 select option (#sub1 or secon function of javascript) n't work. thank before.

我想用select选项进行步进输入,在这种情况下,我做3步,当选择第一个选项然后将显示下一个选项2,然后选项3.我使用ajax并且我设置$ config ['csrf_protection'] = TRUE ;在Codeigniter配置文件中。在第一个选择选项(#kategori)工作并在第二个选择选项中显示下一个值,但在步骤3中选择选项(javascript的#sub1或secon函数)不起作用。谢谢你们。

This is my 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 as $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 the response json array 'kategori_sub1'
        {
            $('#sub1').empty();
            $('#sub1').show();
            $.each(kategori_sub1,function(id,sub1) 
            {
                var opt = $('<option />'); // creating a new select option for 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>

1 个解决方案

#1


0  

I suspect your problem is using wrong event mouseout.

我怀疑你的问题是使用了错误的事件mouseout。

You should be using change the same way the first one that works does.

您应该以与第一个有效的方式相同的方式使用更改。

Beyond that more information is needed about the actual requests by inspecting in browser dev tools

除此之外,还需要通过浏览器开发工具检查实际请求的更多信息

#1


0  

I suspect your problem is using wrong event mouseout.

我怀疑你的问题是使用了错误的事件mouseout。

You should be using change the same way the first one that works does.

您应该以与第一个有效的方式相同的方式使用更改。

Beyond that more information is needed about the actual requests by inspecting in browser dev tools

除此之外,还需要通过浏览器开发工具检查实际请求的更多信息