Home >Backend Development >PHP Problem >How to determine whether a post is submitted in php
php method to determine whether a post is submitted: 1. Create a php sample file; 2. Query whether "$_SERVER['REQUEST_METHOD']=POST" is true through the "if" statement and obtain the data submitted by POST Just process and submit.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
In web development, we usually collect user-entered data through forms and send this data to the server for processing. Forms can use two methods to submit data: GET and POST.
The GET method passes the data to the server through the URL, and the submitted data can be seen in the URL, which is less secure; the POST method encapsulates the data in the HTTP request body and sends it to the server, which can hide the data. Higher security. Among them, the POST method is often used to submit form data.
In PHP, we can obtain the data submitted by the form through the $_POST
global variable. This variable is an associative array, its subscript is the name attribute value of the input element in the form, and its value is the data entered by the user.
So how to determine whether there is currently a POST request? We can determine whether the current request method is POST by judging whether the value of $_SERVER['REQUEST_METHOD']
is 'POST'.
The following is a sample code that shows how to determine whether there is currently a POST request and obtain the data submitted by POST:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 获取POST提交的数据 $username = $_POST['username']; $password = $_POST['password']; // 处理表单数据 // ...} else { // 显示页面 // ...}
In the above sample code, first determine whether the method of the current request is POST, if yes, obtain the data submitted by POST and process it; otherwise, display the page.
The above is the detailed content of How to determine whether a post is submitted in php. For more information, please follow other related articles on the PHP Chinese website!