Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form sends GET instead of POST

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.

like image 556
Mirakurun Avatar asked Jun 19 '16 13:06

Mirakurun


People also ask

Can the form be submitted using GET instead of POST?

As with submitting any form data, you have the option of submitting your data in the form of GET requests, and you will save a few lines of code if you do so. However, there is a downside: some browsers may cache GET requests, whereas POST requests will never be cached.

Which form is get or POST?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

Why would you use get over POST?

The main difference is that POST is used to modify the state of the server application, while GET only requests data from it. The difference matters when you use clean, "restful" URLs, where the URL itself specifies the resource, and the different methods trigger different actions on the server side.

What happened when you send data from a HTML form with GET method?

The GET method is the method used by the browser to ask the server to send back a given resource: "Hey server, I want to get this resource." In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.


1 Answers

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]
like image 156
Anwar Avatar answered Sep 18 '22 16:09

Anwar