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.
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.
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With