如何通过jQuery AJAX在CodeIgniter中发送多个复选框?

时间:2017-09-18 02:43:52

标签: jquery html ajax codeigniter

我的代码有问题为什么然后我点击按钮在网址中简单地回复:

在url输出:

localhost / rootappl / moduls / krs myCheckboxes%5B%5D = KFT106& myCheckboxes%5B%5D = KFT107& submit = Simpan

HTML CODE:

[IMG] http://i65.tinypic.com/21lpzk9.png[/IMG]

HTML代码:

<form id="input_krs" class="input_krs" method="post" name="input_krs" action="#"> 
    <table width="100%" border="1" cellpadding="0" cellspacing="0" class="tabel" class="table table-bordered">
        <tbody>
      <tr valign="middle" class="head" style="background-color: #f4f4f4;">
          <td width="2%"><center>No</center></td>
          <td width="12%"><center>KODEMK</center></td>
          <td><center>MATA KULIAH</center></td> 
          <td><center>SKS</center></td>
          <td><center>Jadwal</center></td>  
          <td><center>Aksi<input type="checkbox" class="checkbox" id="select_all"></center></td>  
          </tr> 
          <tr>
            <td align="center">1</td>
            <td align="center">KFT106</td>
            <td>MATEMATIKA DASAR I</td>
            <td align="center">3</td>
            <td>RABU, Jam:10.15, Kelas:A, Ruang:207 Astri Charolina, S.KOM</td>
            <td align="center"><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="KFT106" class="checkbox"></td>
          </tr> 
          <tr>
            <td align="center">1</td>
            <td align="center">KFT107</td>
            <td>MATEMATIKA DASAR I</td>
            <td align="center">3</td>
            <td>RABU, Jam:10.15, Kelas:A, Ruang:207 Astri Charolina, S.KOM</td>
       <td align="center"><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="KFT107" class="checkbox"></td>
            </tr>  
          <tr>            
          <td colspan="6" align="center">&nbsp;</td>
          </tr>  
      </tbody>
      <br>  
    </table>
    </form> 

<div class="box-footer">
<tr>         
      <td align="center">
     <center>
      <input type="submit" id="submit" class="btn btn-primary" name="submit" value="Simpan" onclick="submitForm()" /> </center></td>
  </tr> 
<br>   
</div>

回应:

 <div id="myResponse"></div>

Jquery ajax代码:

<script type="text/javascript">   

$(document).ready(function(){  

function submitForm() { 
var form = document.myform;
var dataString = $(form).serialize();    
$.ajax({    
    type:'POST', 
    url  : "<?php echo site_url(); ?>krs/save",
    data: 'dataString',
     // data: {dataString : dataString},
    success: function(data){
        $('#myResponse').html(data);
        console.log(dataString);
        alert(" Hello! I am an alert box!! ");
    },
        'error': function(response) {
             //echo 'error 1 ';
        }
});
return false;
} 
}); 
</script> 

打印日志:

 <?php 

    echo var_export($_POST);

    ?> 

控制器:

<?php
  public function save()
  {
      $x =  $this->input->post('myCheckboxes');
      printr($x);
  }
?>

可以帮助我吗?

Br,谢谢。

2 个答案:

答案 0 :(得分:0)

请检查此代码并让我知道它的工作情况?你在两个错误的输入上采用相同的id。首先,您必须通过$ _POST查看从ci上的ajax获取的数据。 我从提交按钮中删除了函数调用。

<script type="text/javascript">
$(document).ready(function(){  
  $('#input_krs').on('submit', function(e) {
    var dataString = $('#input_krs').serializeArray();   
    $.ajax({    
        type:'POST', 
        url  : "<?php echo site_url(); ?>krs/save",
        data: {sendval: dataString },
         // data: {dataString : dataString},
        success: function(data){
            $('#myResponse').html(data);
        },
        'error': function(response) {
             //echo 'error 1 ';
        }
    });
  });
}); 
</script>

在控制器KRK中

<?php
  public function save()
  {
      if($_POST){
        print_r($_POST);
        $x =  $this->input->post('sendval');
        echo "<pre>";print_r($x); die;
      }
  }
?>

答案 1 :(得分:0)

我的控制器代码:

public function save(){
        //$x =  $this->input->post('myCheckboxes');
        //printr($x);



        //$rows =  $this->input->post('rows');
        //foreach ($rows as $key => $value) {
        //  $arr =  array(
        //      'id_mahasiswa'=>$value['mhs'],
        //      'id_kurikulum'=>$value['kurikulum'],
        //      'krs_create_date'=>date('Y-m-d H:i:s'),
        //  );
        //  $this->general->save($arr,'tt_krs');
        //}

        if($_POST){
        print_r($_POST);
        $x =  $this->input->post('sendval');
        echo "<pre>";print_r($x); die;
      }

Image HMVC Root appl

https://www.photobox.co.uk/my/photo/full?photo_id=21810643520