We use the predefined $_GET variable to collect values in a form with method=”get”. The information that is hold by get method is visible to everyone because we can see it in browser’s address bar. It has limits on the amount of the information to send. If you want to send some important data such as password don’t use get method for security. Next lesson I am gonna talk about post method, it is useful in these kind of situations. Get method cannot hold very large values. So don’t use this method with values exceeding 2000 characters.
Lets make an example : (I am gonna use the same example in my previous lesson with get method)
Our main.php file should look like this. The only difference between this and previous example is that: here we used method=”get” instead of method=”post”.
Our main.php file look like that:
<html> <head></head> <body> <form action="welcome.php" method="get"> Username: <input type="text" name="username"/> Age: <input type="text" name="age"/> <input type="submit"/> </form> </body> </html>
Our welcome.php file should look like this: (The only difference between this and previous example is that: here we used $_GET instead of $_POST.)
Welcome <?php echo $_GET["username"]; ?>! </br>
You are <?php echo $_GET["age"]; ?> years old.
If we type Mehmet into username input field and 22 into age input field then click on the submit button we should see that output in welcome.php url location:
You are 22 years old.
And our browser’s address bar should look like this:
As you see all datas that we typed into input fields are carried in address bar. So please don’t use this method if you want to send important information or passwords.
In this lesson you’ve learned how to send data with GET method in PHP. Stay cool and stay connected to learn more! 😉