0

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?

1
  • Are you using nameIDs[] because that hidden input is a repeatable form field? If so, then are you (invalidly) creating multiple id=nameIDs in 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 contains nameIds[] : 1106,1135,2110. Is that value somehow unquoted? Commented Aug 14 at 7:18

1 Answer 1

3

AFAICT you are trying to use a Javascript array of numbers (newIDs) to set a single HTML input value. But an input value is always going to be a string. PHP does some magic to convert multiple inputs with the same name[] into an array on the back end, but on the front end, each of those individual inputs is just a string value. In your case, AFAICT, you have a single input, and its value is going to be a string.

So you have 2 choices:

  • create multiple inputs, each with name="nameIDs[]", and have your JS populate the value attribute of each individually to a single value like 1106. Make sure each input has a unique ID, if you're going to use IDs to address them in JS. Then on the back end $_POST['nameIDs'] will be an array of the individual values.

  • OR have a single input, with a name like name="nameIDs", and have your JS populate the value with a string describing all of your values together. How you represent it is up to you, and doesn't matter, as long as you can extract it on the back end. For eg if you go with comma-separated 1106,1135,2110 you can use explode() on the back end.

As you say there are plenty of questions here about this already, but maybe none addressing this particular misconception.

As a side note, if you want to check what a variable looks like, use var_dump() to see both the type and the value. I tried:

JS:

// Simulating your code
var newIDs = [] ;
[1106,1135,2110].forEach(thisID => {
    newIDs.push(thisID) ;
});

// Note the code in your question has a typo here, missing the closing "'",
// I am assuming that's just a typo in the question, not in your real code.
document.getElementById('nameIDs').value = newIDs;

PHP:

<form method="post">
    <input type='hidden' id="nameIDs" name="nameIDs[]" value="">
    <input type='submit' value='Submit'>
</form>

<pre>
    <?php var_dump($_POST); ?>
</pre>

And after submitting the form I see:

array(1) {
    ["nameIDs"]=>
    array(1) {
        [0]=>
        string(14) "1106,1135,2110"
    }
}

So setting your single input value to a Javascript array has actually created a nested array on the back end, and $_POST['nameIDs'][0] is a string of your actual values, 1106,1135,2110.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. The thing that threw me is when I did the 'foreach ($nameIDs as $id) { echo "$id" ; } - it printed just the first value from the form input array implying it knows that '1106` is its own element in the array, I was expecting the foreach loop to then print out the next two values, but it didn't. Based on your explanation I would then would then expect it to print 1106,1135,2110 as the first (and only) element of the array. Thus, what happened to 1135 and 2110.
Though I still don't fully understand what PHP is doing with incoming $_POST value (array). It sees it as single element array but has some how discarded elements #1 and #2 from the incoming array. Anyway, I went with your option #2, a simple fix. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.