PHP Tutorial – PHP $_GET and $_POST

PHP $_GET and $_POST

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

The $_POST Function

The built-in $_POST function is used to collect values in a form with method=”post”.

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Example:

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

The script specifies that the form data will be submitted to post.php using the post method.

When the user click the “Submit” button the URL will look like this:

http://localhost/IT113/phpFile/post.php

Save this script as postprocess.php

Next we are going to create a php file that will display the value entered in the postprocess.php. Here is the script:

<title>Using post method</title>
Welcome <?php echo $_POST["name"];?>.<br/>
You are <?php echo $_POST["age"];?> years old

Save this file as post.php

When to use method=”post”?

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

PHP $_GET Function

The $_GET variable is used to collect values from a form with method=”get”.

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser’s address bar) and it has limits on the amount of information to send (max. 100 characters)

Example:

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

The script specifies that the form data will be submitted to get.php using the get method.

When the user click the “Submit” button the URL will look like this:

http://localhost/IT113/phpFile/get.php?name=Piolo&age=34

Save this script as getprocess.php

Next we are going to create a php file that will display the value entered in the getprocess.php. Here is the script:

<title>Using get method</title>
Welcome <?php echo $_GET["name"];?>.<br/>
You are <?php echo $_GET["age"];?> years old

Save this file as get.php

When to use method=”get”?

When using method=”get” in HTML forms, all variable names and values are displayed in the URL.

Note: This method should not be used when sending passwords or other sensitive information!

However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.