Peter
Peter

Reputation: 21

POST is not getting correctly parsed into $_POST

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.

Upvotes: 0

Views: 471

Answers (3)

eridal
eridal

Reputation: 1338

sometimes PHP needs from us :)


<?php
#$input = file_get_contents('php://input');
$input = urldecode( '%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=' );

$_POST = array();

foreach( explode( '&', $input ) as $entry ) {
    list( $key, $value )  = explode( '=', $entry );

    $_POST[ $key ] = $value;
}

echo $input, PHP_EOL;
print_r( $_POST );


Upvotes: 0

Artefacto
Artefacto

Reputation: 97835

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 '_'
  • ...

Upvotes: 1

Pekka
Pekka

Reputation: 449435

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>

Upvotes: 0

Related Questions