Reputation: 515
I'm using a JQuery form repeater plugin (https://github.com/DubFriend/jquery.repeater) to allow my users to add additional rows to a form. I am part of the way in getting the result back but am now stumped. The documentation is not that great so I've been searching for a way to do this.
I need to get the fields from the form repeater (through $_POST) and seperate them out and add them to my database. I can do the database bit :-)
I've tried some foreach combinations to try and get the entries from the array but am a bit stumped, a little new to arrays!
This is the HTML from the form...
<div class="repeater">
<div data-repeater-list="fuelUplifts">
<div data-repeater-item>
<div class="row">
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="fuelUpliftLocations">Uplifted from</label>
<input type="text" class="form-control" id="fuelUpliftLocations" name="fuelUpliftLocations" maxlength="45" value="<?php echo Input::get('fuelUpliftLocations'); ?>">
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="fuelUpliftAmount">Amount</label>
<div class="input-group">
<input id="fuelUpliftAmount" name="fuelUpliftAmount" type="number" step="0.01" maxlength="8" class="form-control" placeholder="" value="<?php echo Input::get('fuelUpliftAmount'); ?>">
<div class="input-group-append">
<button data-repeater-delete class="btn btn-danger" data-message="yo" type="button"><i class="mdi mdi-delete-forever"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<input class="btn btn-primary" data-repeater-create type="button" value="Add another fuel stop"/>
</div>
If I print_r the data-repeater-list I get the following
array(2) { [0]=> array(2) { ["fuelUpliftLocations"]=> string(26) "Mayflower Marina, Plymouth" ["fuelUpliftAmount"]=> string(4) "1500" } [1]=> array(2) { ["fuelUpliftLocations"]=> string(23) "Yarmouth, Isle of White" ["fuelUpliftAmount"]=> string(4) "1500" } }
I'm still trying to get my head around arrays and the best way of getting their results. Can anyone point me in the right direction to pull out the data from the 'fuelUpliftLocations' and 'fuelUpliftAmount'?
I'd like to take each of the fields and save them in their respective column in the database as a comma seperated string.
It's the getting it out that is stumping me. I've tried some foreach loop variations like:
foreach ($_POST['fuelUplifts'] as $key => $value) {
print_r($value);
}
This returns
Array ( [fuelUpliftLocations] => Mayflower Marina, Plymouth [fuelUpliftAmount] => 1500 ) Array ( [fuelUpliftLocations] => Yarmouth, Isle of White [fuelUpliftAmount] => 1500 )
So I think I'm getting closer. Could anyone point me in the right direction?
Many thanks!
Matt
Upvotes: 0
Views: 586
Reputation: 2102
Is this what you're trying to do?
foreach ($_POST['fuelUplifts'] as $row) {
print $row['fuelUpliftLocations'];
print $row['fuelUpliftAmount'];
}
This is untested, but should print out the location and the amount at for each row. If you need additional formatting you now have control to do so.
Upvotes: 1