2

I'm working on a page in PHP 5.3.5, and it seems that $_POST doesn't contain the data submitted from my form.

This is the HTML file :

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

And this is the PHP file:

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html> 

The problem is that the form values are not passed, so I get output like this:

Welcome !
You are years old.

Instead of this:

Welcome John!
You are 28 years old.

What am I doing wrong?

Tim Stone
  • 19,119
  • 6
  • 56
  • 66
naji
  • 21
  • 1
  • 1
  • 3

9 Answers9

5

Try <?php var_dump($_POST); ?> to see what's really in there. There's no way $_POST is broken - it's gonna be something else. Possibly a server setting could be disabling $_POST.

Nick Locking
  • 2,147
  • 2
  • 26
  • 42
2

It's probably because you have magic_quotes_gpc turned on. You should turn it off: http://www.php.net/manual/en/security.magicquotes.disabling.php

Mike Yo
  • 21
  • 1
2

PHP 5.3 is moving away from using global, pre-set variables, like $_POST, to avoid vulnerabilities.

The issue, as I understand it, is that programmers who have never had to use $_POST or $_GET might treat it as any other variable and open themselves up to security threats.

I haven't discovered the "proper" way to retrieve $_POST data yet, but the method I've using seems fairly sanitary.

<?php parse_url(file_get_contents("php://input"), $_POST); 

This transfers the parsed HTTP POST string to the $_POST variables

SaschaM78
  • 4,376
  • 4
  • 33
  • 42
2

Have you check your php.ini ?
I broken my post method once that I set post_max_size the same with upload_max_filesize.

I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0

zx1986
  • 880
  • 2
  • 12
  • 18
0

Try to use capitals in POST:

<form action="file.php" method="POST">

Instead of:

<form action="file.php" method="post">
0

I also had the same problem.I used windows notepad for editing .php file and accidentally saved the file in Unicode encoding and that was creating the problem. Then I saved it in the UTF-8 encoding, and it worked like a charm. Hope this might help.

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

Is your HTML form method se to to POST?

<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>
msmafra
  • 1,704
  • 3
  • 21
  • 34
0

You have not POSTed, or something is clobbering $_POST.

If you have no mention of $_POST in your code before trying to access them, then you haven't POSTed to your script.

alex
  • 479,566
  • 201
  • 878
  • 984
0

Here it worked, but in PHP 5.3.1. Try this:

<!doctype html>
<html>
<head>
<title>form</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>

</body>
</html>


<?php
if(isset($_POST["txtname"]))
    $txtname = $_POST["txtname"];
else
    unset($txtname);
if(isset($_POST["txtage"]))
    $txtage = $_POST["txtage"];
else
    unset($txtname);
?>
<!doctype html>
<html>
<head>
<title>form</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<p>Welcome <?=$txtname; ?>!<br />
You are <?=$txtage; ?> years old.</p>
</body>
</html>
msmafra
  • 1,704
  • 3
  • 21
  • 34