I have a <select name="id[]">
drop-down within a form on my page. I also have a button that can add more select
boxes (and their containers), via jQuery:
$(document).ready(function() {
// $(".add-blank").click(function() {
$(document).on("click", ".add-blank", function() {
var container = $(this).closest("div");
container.find(".blank-group").last().clone().appendTo(container.find(".blank-container"));
});
});
However, when I POST
this information to a script, and var_dump($_POST['id'])
, the array only has one value, no matter how many items should have been in that array:
array(1) { [0]=> string(5) "21699" }
I'm almost certain it's because the first select box was in the code when the page loaded, but the rest have been added via jQuery after DOM ready.
My question is, how can I overcome this?
As you can see in my jQuery above, I tried changing my basic .click()
to an .on("click")
, but this didn't seem to have any effect.
The basic HTML of my form is this:
<form>
<div class="blank-container">
<div class="form-group blank-group">
<select name="id[]"></select>
</div>
</div>
<button class="add-blank" type="button"></button>
<button type="submit"></button>
</form>
And pressing the .add-blank
button three times extends the form like so:
<form>
<div class="blank-container">
<div class="form-group blank-group">
<select name="id[]"></select>
</div>
<div class="form-group blank-group">
<select name="id[]"></select>
</div>
<div class="form-group blank-group">
<select name="id[]"></select>
</div>
<div class="form-group blank-group">
<select name="id[]"></select>
</div>
</div>
<button class="add-blank" type="button"></button>
<button type="submit"></button>
</form>
If I chose options in those selects to the values 111
, 222
, 333
and 444
respectively, I would expect to POST
the following array:
array(4) { [0]=> string(3) "111", [1]=> string(3) "222", [2]=> string(3) "333", [3]=> string(3) "444" },
But no matter how many/few values I attempt to pass in my id[]
array, only the first value reaches the script, as only one <select>
was on the page when it initially loaded..