如何使用codeigniter从多个输入中获取值

时间:2017-02-21 07:54:34

标签: javascript php codeigniter

向发送控制器发送多个数据时遇到问题。

$ target 是帖子('warna')的结果。我如何获得所有数据?

<form action="http://localhost/starsc/trueaccon2194/opsional" id="form" method="post" accept-charset="utf-8">
  <div class="row">
    <div class="col-md-12 input group kolom">
      <div id="warna1" style="padding-left:0;padding-right:0;" class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" id="warna1" name="warna[]" class="joss form-control">
        <br>
      </div>
      <div id="warna2" style="padding-left:0;padding-right:0;" class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" id="warna2" name="warna[]" class="joss form-control">
        <br>
      </div>
      <div id="warna3" style="padding-left:0;padding-right:0;" class="col-md-12 col-sm-12 col-xs-12">
        <input type="text" id="warna3" name="warna[]" class="joss form-control">
        <br>
      </div>
      <button type="button" class="simpan_warna btn btn-primary">Simpan</button>
</form>
$('.simpan_warna').click(function() {
  var UrlToPass = $("#form").serialize();
  $.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
    type: "POST",
    data: UrlToPass,
    url: baseURL + "trueaccon2194/opsional/proses_tambah_opsi_warna",
    beforeSend: function() {
      $('.simpan_warna').prop('disabled', true);
      $('.simpan_warna').html('sedang menyimpan...'); //Loading button text 
    },
    success: function(success) { // Get the result and asign to each cases
      $('.simpan_warna').prop('disabled', false);
      $('.simpan_warna').html('Simpan'); //reset button text to original text
      alert('Data Berhasil disimpan!');
      $('#modal_form').modal('hide');
      location.reload();
    },
    error: function() {
      $('.simpan_warna').prop('disabled', false);
      $('.simpan_warna').html('Simpan'); //reset button text to original text
      alert('Data gagal disimpan!');
    }
  });
  return false;
});

我的控制器是:

function proses_tambah_opsi_warna(){
    $target = $this->input->post('warna[]');
    if($this->input->post()){
                $data = $this->input->post();
                $data['nama_depan'] = $this->data['nama_depan'];
                $this->opsional_adm->add_warna($data);
                log_helper("warna", "Menambah Warna ".$target."");
            }else{
                log_helper("warna", "Gagal Menambah warna baru");
            }
}

1 个答案:

答案 0 :(得分:1)

尝试获取'warna'帖子值,而不是'warna []':

$target = $this->input->post('warna');

有了这个,$ target将成为一个数组,您将能够使用如下循环访问值:

foreach ($target as $id => $value) {
   echo $value;
}