Matthew2468
Matthew2468

Reputation: 71

Getting Unspecified $_POST Data

I am wondering if it is possible to get POST data without specifying the keys much like one would with print_r($_GET) provided there are parameters in the URL.

for example, if 'my-page.php' had a form action to a page called 'destination-page.php', but the inputs in the forms had random names, how would 'destination-page.php' pick these values up and echo them out? I tried print_r($_POST) with no success.

my-page.php

<!DOCTYPE HTML>
<html>
<body>

<form name="foobar" method="POST" action="destination-page.php">
    Input 1: <input type="text" name="<? echo substr(md5(mt_rand()), rand(4,12)); ?>" />
    Input 2: <input type="text" name="<? echo substr(md5(mt_rand()), rand(4,12)); ?>" />
    <input type="submit" />
</form>

</body>
</html>

destination-page.php

<!DOCTYPE HTML>
<html>
<body>
<?
    $values_array = array();

    foreach($_POST as $val)
        $values_array[$i++] = $val;

    echo(http_post_data('http://www.foobar.com/destination-page.php', $values_array));
?>
</body>
</html>

Any input is appreciated. Obviously, this question is based more on "is this possible?" rather than "is this pragmatic?".

Thank you.

Upvotes: 2

Views: 239

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

Since $_POST is an array, you can use it as any other array too. Though I would change the loop to

foreach($_POST as $val)
    $values_array[] = $val;

For http_post_data you must give data as a string. If you want to use the array, look into http_post_fields instead.

Upvotes: 2

jamis0n
jamis0n

Reputation: 3800

Youre almost there! Try this:

foreach($_POST as $key => $value)  {
    echo "POST " . $key . " = " . $value;   
}

Upvotes: 2

Related Questions