How to use super global variables $_POST and $_GET

王林
Release: 2023-03-03 19:06:01
forward
2404 people have browsed it

How to use super global variables $_POST and $_GET

PHP $_POST is widely used to collect form data. Specify this attribute in the HTML form tag: "method="post".

PHP $_GET is also used Widely used to collect form data, specify this attribute in the HTML form tag: "method="get".

(Recommended tutorial: php graphic tutorial)

Example:

The following example shows an input field (input) and submit button (submit ) form. When a user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute of the

tag.

In this example, we specify the file to process the form data. If you want another PHP file to handle this data, you can modify the specified script file name. We can then use the super global variable $_POST to collect the input field data in the form.

<html>
<body>
 <form method="post" action="<?php echo $_SERVER[&#39;PHP_SELF&#39;];?>">
Name: <input type="text" name="fname">
<input type="submit"></form>
 <?php 
$name = $_POST[&#39;fname&#39;]; 
echo $name; 
?>
</body>
</html>
Copy after login

(Video tutorial recommendation: php video tutorial)

$_GET can also collect the data sent in the URL.

Suppose we have a hyperlinked HTML page containing parameters:

<html>
<body>
<a href="test_get.php?subject=PHP&web=php.com">Test $GET</a>
</body>
</html>
Copy after login

When the user clicks on the link "Test $GET", the parameters "subject" and "web" will be sent to "test_get.php ",You can use the $_GET variable in the "test_get.php" file to get this data.

The following example shows the code of the "test_get.php" file:

<html>
<body>
 <?php 
echo "Study " . $_GET[&#39;subject&#39;] . " @ " . $_GET[&#39;web&#39;];
?>
 </body>
</html>
Copy after login

The above is the detailed content of How to use super global variables $_POST and $_GET. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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