Samuel Masinde
Samuel Masinde

Reputation: 77

Posting data from form Codeigniter

I need to post data from a form that creates fields from looping through some ids, so the fields have the same id and names but different data which i need to pass to my controller an eventually save

My Form View

<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 to post data

$.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();
              });
            }
          });

If i print_r in my controller, I only see the post from the last row

Upvotes: 0

Views: 27

Answers (1)

Manmohan
Manmohan

Reputation: 740

You need to use input name in array format to upload all values

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

and

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

Upvotes: 1

Related Questions