从表格Codeigniter发布数据

时间:2016-05-06 07:45:37

标签: ajax codeigniter

我需要从一个表单中发布数据,这个表单通过一些ID来循环创建字段,因此这些字段具有相同的id和名称,但我需要将不同的数据传递给我的控制器并最终保存

我的表单视图

<form class="form" id="addForm">
              <div class="panel-body">
                <?php foreach ($lab_result->result() as $results){?>
                  <div class="form-group">
                    <div class="col-md-5">
                      <input type="hidden" id="request_details_id" name="request_details_id" class="form-control" value="<?php echo $results->id; ?>"/>
                      <label class="col-sm-12 control-label"><span class="text-warning"><strong>Requested Service &nbsp; : &nbsp; </strong></span> <span class="text-success"><?php echo $results->service_name; ?> - <?php echo $results->service_description; ?></span></label>
                      <label class="col-sm-12 control-label"><span class="text-warning"><strong>Service Costs &nbsp; : &nbsp; </strong></span> <span class="text-success">Ksh. <?php echo $results->service_price; ?></span></label>
                    </div>
                    <div class="col-sm-7">
                      <textarea class="form-control" rows="2" id="results" name="results" placeholder="Input Lab Results, if none type N/A"></textarea>
                    </div>
                  </div>
                  <?php }?>
              </div><!-- panel-body -->
              <div class="panel-footer">
                <button type="submit" class="btn btn-primary">Send Results</button>
              </div>
            </form>

发布数据的Ajax

$.ajax({
            url: '<?php echo base_url(); ?>laboratory/addLabResult',
            type: 'post',
            data: $('#addForm :input'),
            dataType: 'html',   
            success: function(html) {
            $('#addForm')[0].reset();
            bootbox.alert(html, function()
              {
              window.location.reload();
              });
            }
          });

如果我在控制器中使用print_r,我只会看到最后一行的帖子

1 个答案:

答案 0 :(得分:1)

您需要使用数组格式的输入名称来上传所有值

<input type="hidden" id="request_details_id" name="request_details_id[]" class="form-control" value="<?php echo $results->id; ?>"/>

<textarea class="form-control" rows="2" id="results" name="results[]" placeholder="Input Lab Results, if none type N/A"></textarea>