What are the ways to pass values ​​in PHP forms?

藏色散人
Release: 2023-04-05 20:06:01
Original
3604 people have browsed it

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>
Copy after login

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
Copy after login

"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"]; ?>  岁。
Copy after login

As shown:

What are the ways to pass values ​​in PHP forms?

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>
Copy after login

When the user clicks the "Submit" button, the URL is similar to the following:

http://www.php.cn/welcome.php
Copy after login

"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"]; ?>  岁。
Copy after login

As shown:

What are the ways to pass values ​​in PHP forms?

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template