When you visit some website then you surely see some forms to fill out like enter username, password, email and message. These forms are build in html. We use <form> tag in html to submit these forms. There are two methods to submit these types of forms. One method is get and other is post method.
Let me first give you code about how to use get method in html and get values in php.
<?php if(isset($_GET["submit"])){ $username = $_GET["username"]; $password = $_GET["password"]; echo "Username = $username <br>"; echo "Password = $password"; } ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Using Get Method</title> </head> <body> <form method="get" action=""> <p><label for="username">Username: </label> <input type="text" name="username" /></p> <p><label for="password">Password: </label> <input type="password" name="password" /></p> <p><input type="submit" name="submit" value="Submit" /></p> </form> </body> </html>
In the above code we use method=â€get†and then on the top of code we first check if form is submitted by using isset method and in isset method we get the value of submit button. When we first load the page then the php code inside if condition not runs because we have not clicked the submit button. When we fill the username and password then press submit button then php code inside if statement runs and shows the value of username and password which display above the form in browser. The value of action method is left blank because we want to submit the form to current page and no any other page. $_GET[] is used to get the values from the form and in it we pass the name of input fields as we have given in html.
Similarly the code for post method in php is as follows:-
<?php if(isset($_POST["submit"])){ $username = $_POST["username"]; $password = $_POST["password"]; echo "Username = $username <br>"; echo "Password = $password"; } ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Using Post Method</title> </head> <body> <form method="post" action=""> <p><label for="username">Username: </label> <input type="text" name="username" /></p> <p><label for="password">Password: </label> <input type="password" name="password" /></p> <p><input type="submit" name="submit" value="Submit" /></p> </form> </body> </html>
Most of this code is similar to the get method code except we used method=â€post†and we used $_POST[] to get the values from the form.
Now the difference between get and post is that in get method the values of input form fields are shown in the url when we submit the form. While in post method the values of input form fields are not shown in the url. Other difference is that in get method we can pass small number of values i.e we have to use limited number of input fields in one form while in post method we can use large number of input form fields.
For security reason I advise you use post method in most of cases where you want password field to be submitted or any other secure field to be submitted.