There are three common methods of passing parameters in PHP: $_POST[], $_GET[], $_SESSION[], are used to obtain the form, URL and Session respectively Variables value.
1. $_POST[]global variable
Use PHP’s $_POST[]predefined Variable can get the value of the form element, the format is: $_POST[name]
Create a form form.php here, set the method attribute to POST, add a text box, Named user.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>form</title> </head> <body> <form action="index.php" method="post" name="form1"> <input type="text" name="user" /> <input type="submit" name="submit" value="提交" /> </form> </body> </html>
The attribute action within the form directly specifies which page the form content is transferred to. method specifies the method of delivery. post stands for using messaging, just like how we send text messages.
Then we get the form element, the code is as follows:
<?php $user = $_POST['user']; //应用$_POST[]全局变量获取表单元素中文本框的值 echo $user; ?>
Description: In some PHP versions, you can call the value of the form element by directly writing $user. This is the same as php.ini The settings are related. The line of code register_globals=ON/OFF is retrieved in the php.ini file. If it is ON, it can be written directly as $user, but not vice versa. Although it is very convenient to apply the form name directly, there are certain security factors. It is generally recommended to use register_globals=OFF here.
2. $_GET[] global variable
PHP uses the $_GET[] global variable to obtain the data obtained through the GET() method. The value of the form element is in the following format: $_GET[name]
In this way, the value of the form element named name can be used directly.
Use hyperlinks to pass parameters. Many of our online operations involve clicking on hyperlinks to jump between web pages. Parameters can also be passed while clicking.
Create a form form.php here, set the method attribute to GET, add a text box, and name it user.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>form</title> </head> <body> <form action="index.php" method="get" name="form1"> <input type="text" name="user" /> <input type="submit" name="submit" value="提交" /> </form> </body> </html>
The action attribute in the form directly specifies which page the form content is transferred to. method specifies the method of passing using get.
Then we get the form element, the code is as follows:
<?php $user = $_GET['user']; //应用$_GET[]全局变量获取表单元素中文本框的值 echo $user; ?>
Note: PHP can use $_POST[] or $_GET[] global variables to get the value of the form element. But it is worth noting that the obtained form element names differ in the case of letters. If friends neglect the capitalization of letters when writing web programs, they will not be able to obtain the value of the form element or an error message will pop up when running.
3. $_SESSION[] variable
Use the $_SESSION[] variable to get the value of the form element. Its format is: $_SESSION[name]
For example, create a form, add a text box, named user, and the code to obtain the form elements is as follows:
<?php $user = $_SESSION['user'] ?>
Use $_SESSION[] to pass The variable value obtained by the parameter method can be used in any page after saving. However, this method consumes system resources. It is recommended that friends use it with caution. The $_SESSION variable can be referred to the PHP Chinese website php session application code example for detailed introduction.
The above is the detailed content of Detailed explanation of common methods of passing parameters in php. For more information, please follow other related articles on the PHP Chinese website!