14

I have the simplest form possible and all I want to do is echo whatever is written in text box.

HTML:

<form action="" method="post">
  <input type="text" name="firstname">
  <input type="submit" name="submit" value="Submit">
</form>

PHP:

if(isset($_POST['submit'])){
  $test = $_POST['firstname'];
  echo $test;
}

The problem is it's not working on my server (it works on another server). Does anyone has an idea what could be wrong? There are other forms on the server and are working fine.

ptinca
  • 334
  • 2
  • 3
  • 13

11 Answers11

32

I had something similar this evening which was driving me batty. Submitting a form was giving me the values in $_REQUEST but not in $_POST.

Eventually I noticed that there were actually two requests going through on the network tab in firebug; first a POST with a 301 response, then a GET with a 200 response.

Hunting about the interwebs it sounded like most people thought this was to do with mod_rewrite causing the POST request to redirect and thus change to a GET.

In my case it wasn't mod_rewrite to blame, it was something far simpler... my URL for the POST also contained a GET query string which was starting without a trailing slash on the URL. It was this causing apache to redirect.

Spot the difference...

Bad: http://blah.de.blah/my/path?key=value&otherkey=othervalue
Good: http://blah.de.blah/my/path/?key=value&otherkey=othervalue

The bottom one doesn't cause a redirect and gives me $_POST!

geoidesic
  • 4,649
  • 3
  • 39
  • 59
  • 3
    I had a form with `action="booking.php"`, which wasn't working. Turns out it was because my .htaccess rewrites .php addresses to remove the extension. changing `action="booking"` did the trick – binaryfunt Nov 24 '16 at 19:18
  • 4
    Your answer saved me a lot of time. In my case, my form had `action="mysite.com/somefolder` and that caused the `$_POST` to be empty. But when I changed the action to `mysite.com/somefolder/` it worked as expected. Thanks a lot – Ahmad Dec 15 '16 at 05:12
  • Very subtle difference, but big impact. Thanks for this! :) – Tim V Aug 10 '18 at 11:14
  • 1
    Trying for a good hour, doing researches, and..... IT WAS A SLAAASH, OMG.... Thanks a lot. – Mousa Alfhaily Aug 31 '18 at 21:25
  • LOL, I just had the same problem for an hour. Gotta have that SLASH when using post ! – CheeseFlavored Mar 28 '19 at 18:15
  • 1
    I have been on this issue for more than a week. Thanks for the answer, as this answer is not in any of the other stackoverflow questions regarding the same issue, I think this should not be a duplicate question. Normally, I don't checkout the duplicate questions since its the duplicate questions :) - Cheers for the answer though – naman1994 Mar 22 '22 at 03:59
27

A few thing you could do:

  1. Make sure that the "action" attribute on your form leads to the correct destination.
  2. Try using $_REQUEST[] instead of $_POST, see if there is any change.
  3. [Optional] Try including both a 'name' and an 'id' attribute e.g.

    <input type="text" name="firstname" id="firstname">
    

  4. If you are in a Linux environment, check that you have both Read/Write permissions to the file.

In addition, this link might also help.

EDIT:

You could also substitute

<code>if(isset($_POST['submit'])){</code>

with this:

<code>if($_SERVER['REQUEST_METHOD'] == "POST"){ </code>

This is always the best way of checking whether or not a form has been submitted

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
Frankline
  • 40,277
  • 8
  • 44
  • 75
10

Dump the global variable to find out what you have in the page scope:

var_dump($GLOBALS);

This will tell you the "what" and "where" regarding the data on your page.

saimeunt
  • 22,666
  • 2
  • 56
  • 61
thefid
  • 167
  • 1
  • 14
  • This is the best solution. file_put_contents('./test.log', print_r($GLOBALS, true)); Gives a lovely print-out of all of the global vars and you can see where the data is arriving. – Adrian Smith Jul 27 '22 at 14:29
8

I also had this problem. The error was in the htaccess. If you have a rewrite rule that affects the action url, you will not able to read the POST variable.

To fix this adding, you have to add this rule to htaccess, at the beginning, to avoid to rewrite the url:

RewriteRule ^my_action.php - [PT]

Angel Aparicio
  • 374
  • 2
  • 11
7

Instead of using $_POST, use $_REQUEST:

HTML:

<form action="" method="post">
  <input type="text" name="firstname">
  <input type="submit" name="submit" value="Submit">
</form>

PHP:

if(isset($_REQUEST['submit'])){
  $test = $_REQUEST['firstname'];
  echo $test;
}
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

FYI:
$_POST in php 5.3.5 does not work
PHP POST not working

Community
  • 1
  • 1
zx1986
  • 880
  • 2
  • 12
  • 18
1

try this
html code

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

php code:

if(isset($_POST['Submit'])){
$firstname=isset($_POST['firstname'])?$_post['firstname']:"";
echo $firstname;

}
Mugunthan S
  • 538
  • 3
  • 8
  • 21
1

try doing var_dump($_GLOBALS).

A potential cause could be that there is a script running before yours which unsets the global variables. Such as:

unset($_REQUEST);

or even.

unset($GLOBALS);

This could be done via the auto_prepend_file option in the php.ini configuration.

max
  • 96,212
  • 14
  • 104
  • 165
0

There is nothing wrong with your code. The problem is not visible form here.

  1. Check if after the submit, the script is called at all.

  2. Have a look at what is submitted: var_dump($_REQUEST)

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
-2
  1. html file and php file both should reside in htdocs folder in c:\apache2 (if you use apache web server).
  2. Open html file by typing http://"localhost/html_file_name.html"
  3. Now enter Your data in fields.. Your code will run..
demongolem
  • 9,474
  • 36
  • 90
  • 105
-3

Try get instead for test reasons

<form action="#?name=test" method="GET">
  <input type="text" name="firstname" />
  <input type="submit" name="submit" value="Submit" />
</form>

and

if(isset($_GET)){
    echo $_GET['name'] . '<br>';
    echo $_GET['firstname'];
}
Stevanicus
  • 7,561
  • 9
  • 49
  • 70