【问题标题】:How to send multiple checkboxes in CodeIgniter via jQuery AJAX?如何通过 jQuery AJAX 在 CodeIgniter 中发送多个复选框?
【发布时间】:2017-09-18 02:43:52
【问题描述】:

我的代码有问题,为什么然后我点击按钮简化 url 中的响应:

在 url 中输出:

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

HTML 代码:

[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);
  }
?>

可以帮助我吗?

兄弟,谢谢。

【问题讨论】:

  • 请帮帮我...
  • 有人能帮帮我吗?
  • 你只想要复选框值吗?
  • 把你的 html 作为代码在这里而不是在图像中
  • @AnandPandey,完成

标签: jquery html ajax codeigniter


【解决方案1】:

请检查此代码并告诉我它是否有效?您在两个错误的输入上使用相同的 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;
      }
  }
?>

【讨论】:

  • 不起作用,输出中的操作在 url 中相同:localhost/rootapp/controllers/…
  • 我已经编辑了我的答案。首先,您会看到数据是否来自 ajax,因此请使用 print_r($_POST);在保存功能中。
  • 未解决,同样的问题,页面无法重定向到控制器:krs/save。代码没有 print_r($_POST),页面同样的问题@Anand Pandey
  • 这意味着错误在您的控制器或路由中。在此处提供完整的控制器代码。
  • 我的控制器是你的代码共享,在我的应用程序中是 hmvc ..这是图像 hmvc 模块
【解决方案2】:

我的控制器代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 2021-06-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2012-09-08
    相关资源
    最近更新 更多