Home > Article > Backend Development > What are the ways to pass values in PHP forms?
This article mainly introduces two ways to pass values in PHP forms, one is through the $_GET variable, and the other is through the $_POST variable. (Related recommendation: "PHP Tutorial")
Below we will introduce to you two ways to pass values in PHP forms based on specific PHP code examples.
1. $_GET
In PHP, the predefined $_GET variable is used to collect values from the form with method="get".
The form.html file code is as follows:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="welcome.php" method="get"> 名字: <input type="text" name="fname"> 年龄: <input type="text" name="age"> <input type="submit" value="提交"> </form> </body> </html>
When the user clicks the "Submit" button, the URL sent to the server is as follows:
http://www.php.cn/welcome.php?fname=php&age=3
"welcome.php" The file can now collect form data through the $_GET variable (please note that the name of the form field will automatically become the key in the $_GET array):
欢迎 <?php echo $_GET["fname"]; ?>!<br> 你的年龄是 <?php echo $_GET["age"]; ?> 岁。
As shown:
When using method="get" in an HTML form, all variable names and values will be displayed in the URL.
Note: So this method should not be used when sending passwords or other sensitive information!
2. $_POST
In PHP, the predefined $_POST variable is used to collect values from the form with method="post".
form.html file code is as follows:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="welcome.php" method="post"> 名字: <input type="text" name="fname"> 年龄: <input type="text" name="age"> <input type="submit" value="提交"> </form> </body> </html>
When the user clicks the "Submit" button, the URL is similar to the following:
http://www.php.cn/welcome.php
"welcome.php" The file can now Form data is collected through the $_POST variable (please note that the name of the form field will automatically become the key in the $_POST array):
欢迎 <?php echo $_POST["fname"]; ?>!<br> 你的年龄是 <?php echo $_POST["age"]; ?> 岁。
As shown:
The information sent from the form with the POST method is invisible to anyone, and there is no limit on the amount of information sent.
However, since the variable does not appear in the URL, the page cannot be bookmarked.
This article is an introduction to the two ways to pass values in PHP forms, $_GET and $_POST. I hope it will be helpful to friends in need!
The above is the detailed content of What are the ways to pass values in PHP forms?. For more information, please follow other related articles on the PHP Chinese website!