tags:

views:

74

answers:

3

I have a form, and it outputs this into POST:

<?php print file_get_contents('php://input'); ?>

%2Ffaults%2Ffault%5B1%5D%2F%40signal=gnc.gnc_in.ttag_sec&%2Ffaults%2Ffault%5B1%5D=increment&%2Ffaults%2Ffault%5B1%5D%2Fboolean%2F%40value=false&%2Ffaults%2Ffault%5B1%5D%2Fincrement%2F%40value=6677&%2Ffaults%2Ffault%5B1%5D%2Fthreshold%2F%40condition=&%2Ffaults%2Ffault%5B1%5D%2Fthreshold%2F%40value=&%2Ffaults%2Ffault%5B1%5D%2Ftimestamp%2F%40value=

Once urldecoded:

/faults/fault[1]/@signal=gnc.gnc_in.ttag_sec
/faults/fault[1]=increment
/faults/fault[1]/boolean/@value=false
/faults/fault[1]/increment/@value=6677
/faults/fault[1]/threshold/@condition=
/faults/fault[1]/threshold/@value=
/faults/fault[1]/timestamp/@value=

however, when I look in $_POST, what I get is:

   <?php print_r($_POST); ?>
Array ( [/faults/fault] => Array ( [1] => ) )

As you can see, a fair bit is missing from that array. Any ideas why?

Thanks.

A: 

PHP is choking on the [1] in the variable name, which it seems to be interpreting as an array element.

I don't think these are valid field names in HTML anyway?

Depending on what you want to do, I would get rid of the [1] or put it to the end of each variable name, which should result in a number of arrays whose first element contains the desired value.

Something like this

faults/fault[1]/@signal=gnc.gnc_in.ttag_sec<br>
/faults/fault[1]=increment&/faults/fault[1]/boolean/@value=false<br>
/faults/fault/increment/@value[1]=6677<br>
/faults/fault/threshold/@condition[1]=<br>
/faults/fault/threshold/@value[1]=<br>
/faults/fault/timestamp/@value[1]/=<br>
Pekka
They are valid field names (not ids, but names are fine), and treating form controls with `[foo]` in the name is normal PHP form parsing behaviour.
David Dorward
Not only normal, but useful with, for example, `<select multiple>`. (I don't say it wouldn't be useful to be able to switch this off...)
MvanGeest
An astute observer might notice that these names are in fact XPaths ;)It works on another page that has [some#] in the name. I'm not sure why it chokes on this page. Is there some reason PHP would choke on this input when parsing it into $_POST?
Peter
+1  A: 

You have to either restructure the form to something like:

<input name="xpath_expre[]" type="text" value="/faults/fault[1]/@signal" />
<input name="xpath_resul[]" type="text" value="gnc.gnc_in.ttag_sec" />

Or you have to parse the data yourself.

This is the code that's failing on how by trying to build an array when it fins [. As you can see, there are other points of failure on your current approach (these are comments in the code):

  • ignore leading spaces in the variable name
  • ensure that we don't have spaces or dots in the variable name
  • PHP variables cannot contain '[' in their names, so we replace the character with a '_'
  • ...
Artefacto
Peter
A: 
Eridal