Jump to content

form method="post" doubling in $_GET and $_POST


jamInTheValleys

Recommended Posts

Hello everyone,

 

I have a form on a page set to method="post".

 

If I set the action parameter to the same file as the one the form is on it puts the request in both $_POST and $_GET. This is wrong isn't it?

 

Even weirder:

If I loop through $_POST and $_GET and echo the result:

 

echo '<h1>Post</h1>';
foreach ($_POST as $key => $value) {
echo '<p>'.$key.':'.$value.'</p>';
}
echo '<h1>Get</h1>';
foreach ($_GET as $key => $value) {
echo '<p>'.$key.':'.$value.'</p>';
}

 

It displays:

 

Post
input_name:input_value
Get
input_name:input_value

 

But if I also loop through $_REQUEST:

 

echo '<h1>Post</h1>';
foreach ($_POST as $key => $value) {
echo '<p>'.$key.':'.$value.'</p>';
}
echo '<h1>Get</h1>';
foreach ($_GET as $key => $value) {
echo '<p>'.$key.':'.$value.'</p>';
}
echo '<h1>Request</h1>';
foreach ($_REQUEST as $key => $value) {
echo '<p>'.$key.':'.$value.'</p>';
}

 

It displays:

 

Post
input_name:input_value
Get
Request
input_name:input_value
input_name:input_value

 

This behaviour seems odd to me. Can anyone explain what's going on?

 

It behaves as expected when the action is set to a different page.

 

Thanks

Post your form code, and the results of:

echo '<pre>'; // Formatting
echo print_r($_POST) . "<br/>\n";
echo print_r($_GET) . "<br/>\n";
echo print_r($_REQUEST) . "<br/>\n";

 

Should give you a better idea of what's going on.

 

<html>
<body>
<form action="form.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<?php
//ini_set('display_errors',1);
//error_reporting(E_ALL);
echo '<pre>';
echo print_r($_POST, 1) . "<br/>\n";
echo print_r($_GET, 1) . "<br/>\n";
echo print_r($_REQUEST, 1) . "<br/>\n";
?>

 

Returns:

Array

(

    [fname] => d

    [age] => d

)

Array

(

)

Array

(

    [fname] => d

    [age] => d

)

 

As expected.

 

This is wrong isn't it?

 

Again, purely depends on your form code, browser and PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.