I'm using a number of HTML input fields to post a set of day-price values from a form to PHP. My minimal HTML looks like this:
<input type="text" name="prices[0.25]">
<input type="text" name="prices[0.5]">
<input type="text" name="prices[1]">
<input type="text" name="prices[2]">
<input type="text" name="prices[3]">
<input type="text" name="prices[4]">
The key indicates the amount of days (can also include half days etc.), and the values should be the prices for their respective amount of days.
However, when I input some values and use var_dump($_POST) to check what gets posted, I get a normal sequential array, containing integers 0-4 as keys, as opposed to the strings '0.25', '0.5' and so on.
Here's some example input and output:
Input (not that the values are the same as the keys for convenience):
<input type="text" name="prices[0.25]" value="0.25">
<input type="text" name="prices[0.5]" value="0.5">
<input type="text" name="prices[1]" value="1">
<input type="text" name="prices[2]" value="2">
<input type="text" name="prices[3]" value="3">
<input type="text" name="prices[4]" value="4">
Output ($_POST):
array (size=5)
0 => string '0.5' (length=3)
1 => float 1
2 => float 2
3 => float 3
4 => float 4
As you can see, the 0.25 has disappeared and the 0.5 input was mapped to 0. Likely both inputs get mapped to 0 and 0.5 overrides 0.25.
When I check the same input using JS however (console.log($('form').serializeArray());), the results look as expected: Screenshot.
So my questions are:
- Is this intended behavior of PHP?
- If so, what is the best workaround?
- If not, what am I doing wrong?
Thanks!