I have read quite a few posts on this, most provide various solutions but none are really addressing the underlying "why"? I can't figure out PHPs behavior when processing a single $_POST array. Thus, this question hasn't been answered elsewhere (sufficiently).
In my form I have a single hidden input field (not multiple or looped):
<input type='hidden' id="nameIDs" name="nameIDs[]" value="">
And in in my JS I have:
var newIDs = [] ;
...
...
newIDs.push(thisID) ;
...
...
document.getElementById('nameIDs').value = newIDs
When the form is submitted I can see three values in the payload:
nameIDs[] : 1106,1135,2110
When PHP gets the info:
$nameIDs = $_POST['nameIDs'] ;
echo count($nameIDs)
// outputs "1"
// but the count should be 3
OK, so thinking PHP is seeing the entire nameIDs as a string with commas I do:
$nameIDs = explode(",",$_POST['nameIDs']) ;
echo count($nameIDs) ;
// but this errors out on the `explode` saying that argument #2
// must be a string, but an array was given
Ok, so what's up...is this thing a string or an array? If I do:
$nameIDs = $_POST['nameIDs'] ;
echo $nameIDs ;
// It prints out the warning "Array"
// with no actual values.
So it appears it does think it's an array, but only with one value, so next I try:
$nameIDs = $_POST['nameIDs'] ;
foreach ($nameIDs as $id) {
echo "This id is: $id" ;
}
// it prints out just '1106', but not the other two values
So... PHP is seeing an array with only 1 value in it... what happened to the other two values that I can clearly see are being passed to it? What am I missing here?
nameIDs[]because that hidden input is a repeatable form field? If so, then are you (invalidly) creating multipleid=nameIDsin your HTML document? There are multiple problem within this one question. You have a typo. We don't know what is being yatta-yatta'ed in your javascript code. Your PHP array access should be looking in the [0] element of the first level if you are going to use nameIDs[]. I think we need to more thoroughly understand the exact server-side payload that you are claiming containsnameIds[] : 1106,1135,2110. Is that value somehow unquoted?