Regarding the backend (From here):
input type="text" name="hoursWorked[]" />
will internally convert to an array under $_POST['hoursWorked']
.
That means you can do something like this with the form:
<form method="post" action="collect_vals.php">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
<div><input type="text" name="mytext[]"></div>
<div><input type="text" name="mytext[]"></div>
<div><input type="text" name="mytext[]"></div>
<div><input type="text" name="mytext[]"></div>
</div>
</form>
Then, in PHP:
<?php
if(isset($_POST["mytext"])){
$capture_field_vals ="";
foreach($_POST["mytext"] as $key => $text_field){
$capture_field_vals .= $text_field .", "; //Here is where the values are concated to $capture_field_vals
}
echo $capture_field_vals;
}
?>
Regarding the dynamic form (From here):
The JavaScript:
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
The HTML form itself:
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
</div>