4
<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>

This code should print whatever is enter in text box name="ss" when click submit.
But its not printing. Working with method="get" but not with post, What's the problem.

liz
  • 830
  • 14
  • 32
Wasim A.
  • 9,660
  • 22
  • 90
  • 120

10 Answers10

16

If you're just refreshing the page, do:

action=''

instead of:

action="<?php echo $_SERVER['PHP_SELF'];?>"

Also, add this to line 2 to see what's being stored (if anything) in the $_POST array:

var_dump( $_POST );

Hmm... so it's empty on submit? Try adding this to the top of your php file:

if(empty($_SERVER['CONTENT_TYPE']))
{ 
  $_SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded"; 
}

Okay, now check your php.ini (normally requires sudo or root in /etc):

post_max_size = 8M
variables_order = "EGPCS"

Do you have those two rules set? If so, be careful of how much memory you're allocating. Anything over 2048MB could start to give you trouble, depending on your system specs.

NOTE: If you make changes to your php.ini file and PHP is running as an apache module, you'll need to restart apache. Something along the lines of:

sudo /etc/init.d/httpd restart
Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
6

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
6

It may be due to rewrite rules in the .htaccess file.Add this condition to your .htaccess file

RewriteCond %{REQUEST_METHOD} !POST [NC]

OR add this line

 RewriteRule ^welcome_post.php - [PT]
Nidhin
  • 1,818
  • 22
  • 23
  • 2
    Thank you so much! I ran into this problem, and your first rewrite condition fixed it perfectly! Saved me hours of pain :) – julianwyz Feb 17 '15 at 21:23
1

Finally ...Got it.... Firstly I have an Article folder in my htdocs file with welcome.php as the php file in focus and a main HTML file , whose contents we will be discussing about. Here's what worked for me.. Turns out the problem was not XAMPP or any of its related files.. The problem was this :

<form action="welcome.php" method="post">

With this code whenever I pressed the submit button, the url redirects to here file:///C:/xampp/htdocs/Article/welcome.php, Which is not what it needs to be..It has to be a localhost link ..So I changed the value of action attribute to localhost form and now it looks like this

<form action="https://localhost/Article/welcome.php" method="post">

That did the trick..Now the submit button redirects to https://localhost/Article/welcome.php , this link..Which is exactly what is needed.. Remember the browser may ask you for some permission issues ..Just accept them it will run fine... Best of luck.. P.S : This one is for Windows..Hope it will work also in Linux and Mac.

Subham Debnath
  • 689
  • 8
  • 9
0

My friend ran into this problem today. The answer was pretty simple - basically, you have to capitalize the POST part of method="POST"

The final result should look like

<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>
mjkaufer
  • 4,047
  • 5
  • 27
  • 55
  • 2
    this can't be it... see http://www.w3schools.com/php/showphp.asp?filename=demo_form_post – m13r Sep 15 '16 at 08:05
  • 4
    Not true. [Enumerated attributes (like `method`) are case-insensitive](https://www.w3.org/TR/html5/infrastructure.html#enumerated-attribute). – Quentin Jul 31 '17 at 10:02
0

First make sure that your web service (GET/POST etc) is acting as desired using the Chrome Advanced Rest Client. Then you should check your PHP part.

zeeawan
  • 6,667
  • 2
  • 50
  • 56
0

I solved mine with including following into header.

Content-Type: application/x-www-form-urlencoded

Just use this in the header while making a request and my problem was solved.

-1

<form action="" method="post"> method="post" is important for POST Data.

Use PHP REQUEST instead:

<form action="" method="post">
  <input type="email" name="mail">
  <input type="submit" name="submit" value="Submit">
</form>
PHP:

if(isset($_REQUEST['submit'])){
  $val= $_REQUEST['mail'];
  echo $val;
}
JWC May
  • 605
  • 8
  • 14
  • "Never forget to give POST Method in html form attribute" — They didn't. You can clearly see it in the code in the question. – Quentin Jul 31 '17 at 10:00
  • The method="post" will not work if the Old Cache is stored in the Web Browser. To become method="post" active, the Client must verify that POST Method is working within the Cache Storage. – JWC May Aug 01 '17 at 05:55
-2

use this instead;

$variable_name = $_REQUEST["ss"];
echo $variable_name;
Shema_Thomas
  • 89
  • 1
  • 2
-2

change your IDE ,i use phpstorm ,it's fantastic but when i use dreamweaver it works probably, for test you can run your page directly from wampserver localhost , i change default port of apache and i think problem is from there , if you use phpstorm or change port of apache server change your IDE.

samy
  • 1