如何使用Ajax将动态数据发送到控制器codeigniter

时间:2018-11-01 09:36:23

标签: javascript ajax codeigniter-3

我有一个包含用于添加数据的循环输入标签的表单。循环形式是可行的,问题是我不知道如何在控制器代码点火器中检索该循环数据。请帮忙!

这是我的观点(窗体)

Sub x()

Dim lr As Long
Dim sh As Integer
Dim ii As Long

lr = 212 'Cells(212, 2)

For sh = 1 To Worksheets.Count
    For ii = lr To 1 Step -1
        If IsError(Worksheets(sh).Cells(ii, 2)) Then
            If Worksheets(sh).Cells(ii, 2) = CVErr(xlErrNA) Then
                Worksheets(sh).Rows(ii).EntireRow.Delete
            End If
        End If
    Next ii
Next sh

End Sub

这是我的观点(Ajax函数)

<form action="#" id="ap_data">
  <div class="table-responsive">
   <table class="table table-striped table-hover" id="mkt">
    <thead>
     <tr>
      <th>KR</th>
      <th>NP</th>
      <th>KP</th>
     </tr>
    </thead>
    <tbody>
    <?php $number =0;
    $id = "id";
    $idkr = "idkr";
    $np = "np";
    $idkp = "idkp";?>
    <?php foreach ($perb as $value) { 
    $number++; ?>
    <tr>
       <input type="hidden" name="<?php echo $id.$number; ?>" value="<?php echo $value->idp ?>"/>
      <td>
       <input type="text" placeholder="" name="<?php echo $idkr.$number; ?>" value="<?php echo $value->idkp ?>" class="form-control">
      </td>
      <td>
       <select name="<?php echo $np.$number; ?>" class="form-control">
        <option value="1">1 (AS)</option>
        <option value="2">2 (Bold)</option>
        <option value="3">3 (Seed)</option>
       </select>
      </td>
      <td>
       <input type="text" placeholder="" name="<?php echo $idkp.$number; ?>" value="<?php echo $value->idkp ?>" id="class="form-control">
      </td>
    </tr>
  <?php } ?>
  </tbody>
 </table>
 </div>
 <div class="form-group">
  <button type="button" onclick="test_kond()" class="btn btn-success col-lg-12">Test Kond</button>
 </div>
</form>

});

这是我的控制器

<script type="text/javascript">
  var test_kond;
  $( document ).ready(function() {
    test_kond = function () {
    var data_ap;
    data_ap = $("#ap_data").serialize();
    $.ajax({
      type: "POST",
      url: "<?php echo site_url('HoldBotCtrl/kond_ajax')?>",
      data: data_app,
      success: function() {
         alert(data_ap);
      }
   });
 };

这是print_r

的结果
  

idkr1Array([idkr1] => [np1] => [idkp1] =>)idkr2Array([idkr2] => [np2] => [idkp2] =>)

该值仍然为空,我需要从$ this-> input-> post();获取数据。 如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我认为serilize()方法创建一个URL编码的字符串,您想在其中通过ajax中的POST方法发送数据。您可以尝试这种方式。

var test_kond;
  $( document ).ready(function() {
    test_kond = function () {
    var data_ap;
    data_ap = new FormData($("#ap_data"));
    $.ajax({
      type: "POST",
      url: "<?php echo site_url('HoldBotCtrl/kond_ajax')?>",
      data: data_app,
      success: function() {
         alert(data_ap);
      }
   });
 };
});

答案 1 :(得分:0)

只需尝试更改字段名称,如下所示:

<form action="#" id="ap_data">
  <div class="table-responsive">
   <table class="table table-striped table-hover" id="mkt">
    <thead>
     <tr>
      <th>KR</th>
      <th>NP</th>
      <th>KP</th>
     </tr>
    </thead>
    <tbody>
    <?php $number =0;
    $id = "id";
    $idkr = "idkr";
    $np = "np";
    $idkp = "idkp";?>
    <?php foreach ($perb as $value) { 
    $number++; ?>
    <tr>
       <input type="hidden" name="data[<?= $number ?>]['id']" value="<?php echo $value->idp ?>"/>
      <td>
       <input type="text" placeholder="" name="data[<?= $number ?>]['idkr']" value="<?php echo $value->idkp ?>" class="form-control">
      </td>
      <td>
       <select name="data[<?= $number ?>]['np']" class="form-control">
        <option value="1">1 (AS)</option>
        <option value="2">2 (Bold)</option>
        <option value="3">3 (Seed)</option>
       </select>
      </td>
      <td>
       <input type="text" placeholder="" name="data[<?= $number ?>]['idkp']" value="<?php echo $value->idkp ?>" id="" class="form-control">
      </td>
    </tr>
  <?php } ?>
  </tbody>
 </table>
 </div>
 <div class="form-group">
  <button type="button" onclick="test_kond()" class="btn btn-success col-lg-12">Test Kond</button>
 </div>
</form>

并在您的控制器中:

function kond_ajax(){
   print_r($this->input->post());
}

我希望这会有所帮助:)