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.

14
  • 1
    What do you see when you print_r($_POST);? Commented Mar 28, 2012 at 20:17
  • 4
    Why are you using $_SERVER['PHP_SELF'], when you could leave the action attribute empty if you want it to submit to itself? Commented Mar 28, 2012 at 20:17
  • Looks ok to me, what does print_r($_POST) show you? Commented Mar 28, 2012 at 20:17
  • i would guess the php_self tag is mucking things up and forcing a 302 Commented Mar 28, 2012 at 20:18
  • 2
    Wasim, are you getting any errors? If not, try adding ini_set('display_errors',1); error_reporting(E_ALL); This will let you see if anything else is stopping your success. Commented Mar 28, 2012 at 20:23

10 Answers 10

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
Sign up to request clarification or add additional context in comments.

15 Comments

What does the var_dump say? array(0)?
i did print_r($_POST), when first time page load its say Array() but when i submit, nothing in array.
Its not working answer is useless. Var dump as matthew has said and tell us the results.
Hmmm... Okay. Did you set up php yourself? Check my edited answer.
@Wasim Change to more then 8MB.
|
7

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]

1 Comment

Thank you so much! I ran into this problem, and your first rewrite condition fixed it perfectly! Saved me hours of pain :)
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

1 Comment

glad I could help :-D
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.

Comments

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>

2 Comments

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.

Comments

-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;
}

2 Comments

"Never forget to give POST Method in html form attribute" — They didn't. You can clearly see it in the code in the question.
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.
-1

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 Comment

The browser will head that automatically given the code in the question. If it didn't, then there would be no way to add it.
-2

use this instead;

$variable_name = $_REQUEST["ss"];
echo $variable_name;

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.