Home  >  Article  >  Backend Development  >  PHP uses 3 predefined variables to quickly obtain form data

PHP uses 3 predefined variables to quickly obtain form data

青灯夜游
青灯夜游Original
2021-08-25 11:53:174077browse

In web development, if you want to obtain information submitted by users, you often need to use forms. Use a form to display the information that needs to be filled in to the user, and then the user enters the information and submits the form; after the form submits the data, it needs to obtain the data for processing. So how can we quickly obtain form data? In fact, PHP has built-in 3 predefined variables (also called super global variables) to obtain. This document will introduce them to you in detail.

First let’s understand the working process of the form:

  • First the form must be displayed to the user, and the user submits the form after inputting the information.

  • Each form will specify a web page. After the user submits the form, this web page will be loaded and responsible for processing the form information.

For example, the content of the form tag in the user.html file below is the simplest form.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>表单提交</title>
	</head>

	<body>
		<form action="user.php" method="">
			用户名: <input type="text" name="user"> <br><br>
			密 码: <input type="password" name="pwd"> <br><br>
			生 日: <input type="date" name="bday"><br><br>
			<input type="submit" value="提交数据">
		</form>
	</body>

</html>

The methods attribute of the form tag is used To specify how to send form data, whether to use the get method (method="get") or the post method (method="post"). Then the form data will be sent to the page specified by the action attribute. This is the user.php page for processing.

According to the different ways of submitting form data, the methods of obtaining form data are also different: get method can use predefined variables $_GET to obtain; post method You can use predefined variables$_POST to obtain it; if you don’t know how the form submits data, you can use predefined variables$_REQUEST to obtain, the data can be obtained in two ways.

Let’s learn about it one by one:

1. Use the predefined variable $_GET to quickly obtain form data (the form form needs to be set to method="get "

During the development process of the program, since the data submitted by the GET method is attached to the URL and sent, the "URL user-passed parameters" type will be displayed in the address bar of the URL. The information is as follows:

http://url?name1=value1&name2=value2 ...
  • name1 and name2 are the names of the form elements (set by the name attribute of the form elements), and value1 and value2 are the values ​​of the form elements. The URL and form elements are separated by "?", and multiple form elements are separated by "&". The format of each form element is "name= value", fixed.

Let’s add the form of the user.html file and look at the address bar of the URL

PHP uses 3 predefined variables to quickly obtain form data

PHP uses 3 predefined variables to quickly obtain form data

You can directly use the predefined variable $_GET in the user.php file to obtain data. The $_GET global variable is an associative array. The key of the array is the value of the form element name. The value of the array is the value of the corresponding form. (Note that all parameters in the URL can be obtained using $_GET.)

<?php
var_dump($_GET);
?>

PHP uses 3 predefined variables to quickly obtain form data

You can use $_GET['key name'] to obtain each parameter one by one. Value of form element:

<?php
header("content-type:text/html;charset=utf-8");
echo "用户名为:".$_GET['user']."<br>生日为:".$_GET['bday'];
?>

PHP uses 3 predefined variables to quickly obtain form data

2. Use the predefined variable $_POST to quickly obtain form data (the form needs to be set to method="post" )

The post method does not rely on the URL and does not display the passed parameter value in the address bar.

$_POST The global variable is also an associative array. The key of the array is the value of the form element name, and the value of the array is the value of the corresponding form.

<?php
header("content-type:text/html;charset=utf-8");
echo "用户名为:".$_POST['user']."<br>生日为:".$_POST['bday'];
?>

PHP uses 3 predefined variables to quickly obtain form data

3. Use the predefined variable $_REQUEST to quickly obtain form data

$_REQUEST The global variable is a variable that contains $_POST , array of $_GET and $_COOKIE, the array structure is similar to $_POST and $_GET.

<?php
header("content-type:text/html;charset=utf-8");
var_dump($_REQUEST);
echo "用户名为:".$_REQUEST['user']."<br>生日为:".$_REQUEST['bday'];
?>

PHP uses 3 predefined variables to quickly obtain form data

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of PHP uses 3 predefined variables to quickly obtain form data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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