Get and post method

preetisoft2

New member
What are the differences between Get and post methods in form submitting.
give the case where we can use get and we can use post methods?
 
Get and post method are use to transfer the data of one page to other.But difference b/w get ant post is that in get method information of data is shown in url but in post method it is not shown why which post method is more secure.in form tag in action attribute we use this method which transfer the data. if although you have any query so plz visit the w3school.com site .where more detail are given.
 
The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
Example

The example below contains an HTML form with two input fields and a submit button:
<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>

When a user fills out the form above and clicks on the submit button, the form data is sent to a PHP file, called "welcome.php":

"welcome.php" looks like this:
<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

Output could be something like this:
Welcome John!
You are 28 years old.
 
The Get will stuff the input in the URL and hence it should not be used to send Passwords. Also, when there are files to be uploaded, the Method for the form should be POST. That is more on the HTML sides and all of the data sent to PHP depends on what you should use on the server.
 
The Get will stuff the input in the URL and hence it should not be used to send Passwords. Also, when there are files to be uploaded, the Method for the form should be POST. That is more on the HTML sides and all of the data sent to PHP depends on what you should use on the server.

Wow, I did not know that. Thanks for the simple answer. Also, if I wanted to take the information from a form or something and put it into my backend PHP code, would I need POST?
 
get and post

The main difference between get and post is:-
we can change the data run time in the post method and in the get method we can not change the data.........
 
The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

The GET method produces a long string that appears in your server logs, in the browser's Location: box.

The GET method is restricted to send upto 1024 characters only.

Never use GET method if you have password or other sensitive information to be sent to the server.

GET can't be used to send binary data, like images or word documents, to the server.

The data sent by GET method can be accessed using QUERY_STRING environment variable.

The PHP provides $_GET associative array to access all the sent information using GET method.

Try out following example by putting the source code in test.php script.

<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

The POST Method

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

The POST method does not have any restriction on data size to be sent.

The POST method can be used to send ASCII as well as binary data.

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

The PHP provides $_POST associative array to access all the sent information using POST method.

Try out following example by putting the source code in test.php script.

<?php
if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">

Name: <input type="text" name="name" />
Age: <input type="text" name="age" />

<input type="submit" />
</form>
</body>
</html>
 
I think it's a best practise to use $_POST method in forms to submit some kind of data to database, so users can't see what's being send.
And using $_GET method is best, when you need to retrieve some information from database.
For example, if you have news.php file and need to read the first item, this solution would be pretty normal: news.php?id=1
 
Back
Top