Form sends GET instead of POST

15,263

Solution 1

One way to solve this is to reinforce your intentions by expliciting formmethod="post", like this:

<button type="submit" formmethod="post" formaction="add_user.php">Submit</button>

Solution 2

If like me you gone from Routed url (like in Laravel) to plain PHP, and wonder why the file /user/store/index.php responds that it receives a GET request using:

<form method="POST" action="/user/store">
  <input type="text" id="user_name" name="user_name" />
  <button type="submit">Create a new user</button>
</form>

Just check it again... Yes you forgot a trailing slash... so /user/store will receives only get, while /user/store/ will receive what you form requested.

Edit

I inspected what Laravel does, and it solves this particular issue with this addition in their .htaccess (check the original file).

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
Share:
15,263

Related videos on Youtube

Mirakurun
Author by

Mirakurun

I like to keep an air of mystery about me.

Updated on September 19, 2022

Comments

  • Mirakurun
    Mirakurun over 1 year

    I have searched the all questions with similar title, no solution for me yet.

    I have a website running on apache2. I need to submit sensitive information through a form and I need to use POST method. Instead of POST, it sends GET request.

    HTML:

    <form action="/add_user.php" method='POST'>
       Enter Username: <input type="email" name="email" required="required" /> <br/>
       Enter password: <input type="password" name="password" required="required" /> <br/>
       <input type="submit" value="submit"/>
    </form>
    

    PHP:

    <?php
    
    $email=$_POST['email']; 
    $password=$_POST['password'];
    //do stuff
    ?>
    

    I have opened Network monitor in Firefox, and the method is confirmed as GET. I have tried to even make it PUT instead of POST, still it sends GET. Also, $email and $password get the values if I change them to $_GET instead of $_POST.

    Any help would be appreciated.

    • Mirakurun
      Mirakurun almost 8 years
      @statosdotcom Thanks for the hint to check submit, never thought it could be problem in it. I googled submit button for POST method and got this: <button type="submit" formmethod="post" formaction="add_user.php">Submit</button> It finally works now.
  • Admin
    Admin about 5 years
    I was going crazy. Thank you. Why do forms work this way??
  • Anwar
    Anwar about 5 years
    Apparently, taking from another Stackoverflow answer, this is because the way the default php server (php - S) and Appache work: if you ask for /user/store, it tried to get the file with this name, but this is not a php file but a folder. Howether, you fix this issue by adding a trailing slash, which by default will try to find the index.php in the folder, so it works well and responds to your router. Apparently, we could be able to tweak the .htaccess file to tell Appache to always add a trailing slash, but 1. This might not be always wanted 2. I do not know how to do it with php - S...
  • Anwar
    Anwar about 5 years
    @M.I.Wright I just added something that might help you if you use Appache as a server. I think php built-in server will not react to this file however.
  • Admin
    Admin about 5 years
    Thank you! Unfortunately, though, that doesn't apply to me -- I was using Python/Flask when I experienced this issue. I'd defined the route without the trailing slash, too, so there was no redirect or anything, which made the GET-not-POST behavior really surprising to me
  • Adam Cook
    Adam Cook over 3 years
    I don't know why this got all the upvotes. This is a force, not a solution. It's the equivalent of writing Important! at the end of a CSS rule. Better to solve the problem instead.
  • StackHola
    StackHola about 2 years
    What would be another anwser then? The following is not applicable in our case