+ 3
What is the difference between get and post method in html?
I can't understand the difference between get and post method in HTML form.
2 Respostas
+ 1
When you click submit on a form with the get method, it will add information from the submitted form, to the action URL.
http://www.yourwebsite.com/action.php?name=Sarah&age=26
In the action.php script, you can the access the data via the $_GET array:
$name = $_GET['name'];
$age = $_GET['age'];
With method as post, it will not create the URL, but instead the action URL will remain unchanged, i.e. http://www.yourwebsite.com/action.php
You will then access the submitted form data as follows:
$name = $_POST['name'];
$age = $_POST['age'];
Questions?
+ 1
Short answer: get will send data in the url and post will send it in the request body. Get and post are only tenuously tied to html; they are http methods. To fully understand the answer to your question, you must first understand what http is.
http is a mechanism to communicate data from one computer to another. This data travels over the wire a electrical impulses so it has to be converted to zeroes and ones (as everything ultimately is). Think of an http request or a response as little plain text files which are converted to 0s and 1s before being sent over the wire.
So, when you click on a link or type a website into your browser's address bar, what ACTUALLY happens is your browser sends a bunch of text (an http request) to a server. The text has to be formatted and organized in a certain way so that the server Can make sense of it (this is why http is a "protocol"). An example get request might be:
GET /lessons?user=123 HTTP/1.1
Host: sololearn.com
this tells the sololearn.com server that you want to GET the lessons, and you want to pass the lessons method the parameter user=123
A sample post request might be:
POST /sendemail.php HTTP/1.1
Host: yourwebsite.com
subject=hello&message=world
(Notice the two newlines between the request headers and body). This tells the server at yourwebsite.com to call the sendemail.php script and pass it two variables in the request body (subject and message).
Html forms are simply another way the browser has to generate requests for your users. If you use method get, the request will say GET and the form data will go in the url. if you use method post, the request will say POST and the form contents will be in the request body. You need to set up your html in whichever way your server is set up to receive the request.
Hopefully that helps!