Quick backstory. I have a portion of a simple form that allows the user to add/remove a row of three inputs. I'd thought originally that I would just use jQuery to show/hide static fields, but I poked around and found a solution that seemed to work really well.
(Reference: http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery/comment-page-1#comment-6093)
As far as the array is concerned, I was working from this: Submitting a multidimensional array via POST with php
HTML
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field">
<button type="button" class="remove-field" title="Remove Product">×</button>
<input type="text" name="part[][number]" class="part-number-input" placeholder="">
<input type="number" name="part[][quantity]" placeholder="">
<input type="number" name="part[][tolerance]" class="" placeholder="">
</div>
</div>
<button type="button" class="add-field" title="Add Product">+</button>
</div>
PHP
if(isset($_POST['part']))
{
$message .='<table>';
$message .='<tr><td><b>Part #</b></td><td><b>Quantity</b></td><td><b>Tolerance</b></td></tr>';
foreach ($_POST['part'] as $parts)
{
$message .='<tr>';
$message .='<td> '.$parts['number'].' </td>';
$message .='<td> '.$parts['quantity'].' </td>';
$message .='<td> '.$parts['tolerance'].' </td>';
$message .='</tr>';
}
$message .='</table>';
}
OUTPUT TO EMAIL
Part # Quantity Tolerance
part1#
part1qty
part1tol
part2#
part2qty
part2tol
I've only ever messed with extremely simple forms, so any help would be greatly appreciated!
UPDATE: Adding the jQ that clones the fields.
JS
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).fadeIn(300).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
Again, I really appreciate the help.