Home Backend Development PHP Problem Summarize the method of receiving form data in php

Summarize the method of receiving form data in php

Apr 04, 2023 am 09:14 AM

PHP is a very powerful server-side scripting language that is widely used in web development. In web development, receiving form data is a very important step, and PHP provides a variety of methods for receiving form data. Let’s take a look at these methods together.

1. Basic form submission method

First of all, the most basic method of submitting a form is to submit it through the

tag in HTML. In the tag, there is an attribute called "method", the default value is "get", you can also use "post" to submit. So what is the difference between these two submission methods?

(1) Get method: Attach the form data to the URL and pass it. The data can be seen in the address bar of the browser. Since the length of the URL is limited, it is suitable for passing shorter data, such as search keywords, etc. The code for receiving form data using the get method is as follows:

<?php
echo $_GET[&#39;name&#39;];
?>
Copy after login
Copy after login

(2) Post method: Pass the form data as part of the HTTP package, and the data will not appear in the address bar. Since the data is transferred through HTTP packets, it is suitable for transferring larger data, such as uploading files, etc. The code for receiving form data using the post method is as follows:

<?php
echo $_POST[&#39;name&#39;];
?>
Copy after login

2. $_GET and $_POST arrays

We can receive form data through $_GET and $_POST arrays. Among them, $_GET is used to receive data submitted by the get method, and $_POST is used to receive data submitted by the post method. Both arrays are PHP superglobal variables and can be accessed from anywhere.

Take receiving a form data named "name" as an example. When the form is submitted using the get method, the PHP code that receives the data is as follows:

<?php
echo $_GET[&#39;name&#39;];
?>
Copy after login
Copy after login

When the form is submitted using the post method, the PHP code that receives the data is as follows:

``
< ?php
echo $_POST['name'];
?>
``

If we don’t know the name of the form data, we can use PHP’s foreach statement to output all submissions The data is as follows:

<?php
foreach ($_POST as $key => $value) {
    echo $key . ':' . $value;
}
?>
Copy after login

3. $_REQUEST array

In addition to the $_GET and $_POST arrays, PHP also provides a global array $_REQUEST. The $_REQUEST array can receive any data submitted using the get or post method, and there is no need to care about which method is used to submit the data. As long as the submitted data has a name, it can be accessed through the $_REQUEST array.

Take receiving a form data named "name" as an example. When submitting a form using the get method or post method, the PHP code to receive the data is as follows:

<?php
echo $_REQUEST[&#39;name&#39;];
?>
Copy after login

It should be noted that the value in the $_REQUEST array may be overwritten by the $_GET or $_POST array, so if there is Duplicate data will be accessed according to the value of $_GET or $_POST.

4. $_SERVER['QUERY_STRING'] variable

In addition to using the $_GET and $_POST arrays to receive form data, we can also use a special variable $_SERVER['QUERY_STRING provided by PHP '] to get the query string in the URL. The query string is the part after "?" in the URL, which includes the parameters used by the website to query data.

For example, for the following URL:

http://example.com/index.php?id=1&page=2
Copy after login

We can get the id and page parameters as follows:

<?php
$query_string = $_SERVER[&#39;QUERY_STRING&#39;];
echo $query_string; // 输出:id=1&page=2
?>
Copy after login

It should be noted that $_SERVER['QUERY_STRING '] variable can only get the query string part of the URL, but cannot get the POST data.

Summary

The above is how PHP receives form data. By using PHP functions such as $_GET, $_POST, $_REQUEST, $_SERVER['QUERY_STRING'], etc., we can easily receive and process form data. Next time you develop a web application, don't forget to choose the method that best suits you for receiving form data.

The above is the detailed content of Summarize the method of receiving form data in php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

PHP Input Validation: Best practices. PHP Input Validation: Best practices. Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

See all articles