POST() method and GET() method for submitting form data in php

伊谢尔伦
Release: 2023-03-07 14:38:01
Original
10350 people have browsed it

Submitting and obtaining form data is the most commonly used operation in form applications. It is often necessary to PHP in the background to obtain various data submitted by users in the foreground form page from the foreground page. There are two ways to transfer form data, one is the POST() method, and the other is the GET() method. The specific method of obtaining data is specified by the method attribute of the

form. The specific application of these two methods in Web forms is explained below.

Use the POST() method to submit the form

When using the POST() method, you only need to change the attributes in the

form method can be set to POST. The POST() method does not depend on the URL and will not be displayed in the address bar. The POST() method can transfer data to the server without restrictions. All submitted information is transmitted in the background. Users cannot see this process on the browser side, and security will be higher. Therefore, the POST() method is more suitable for sending confidential (such as bank account number) or large-capacity data to the server.

The following example will use the POST() method to send text box information to the server. The sample code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<form action="index.php" method="post" name="form1">
  <table width="300" border="0" cellpadding="0"  cellspacing="0">
    <tr>
      <td height="30">订单号:
 <input type="text" name="user" size="20">
        <input type="submit" name="submit" value="提交">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
Copy after login

Note: In the above code, the method attribute of the form form specifies The delivery method of the POST() method, and the data page is specified as index.php through the action attribute. Therefore, when the "Submit" button is clicked, the information in the text box can be submitted to the server, and the running results are as follows:

POST() method and GET() method for submitting form data in php

Use the GET() method Submit the form

The GET() method is the default method of the method attribute in the form. When using the GET() method to submit form data, the data will be appended to the URL and displayed, and sent to the server as part of the URL. During the development process of Cheng Xun, since the form data submitted by the GET() method is attached to the URL and sent, the following content will be displayed in the address bar of the URL: "URL address + parameter information passed by the user".

The parameter passing format of the GET() method is as follows:

POST() method and GET() method for submitting form data in php

where url is the response address of the form (such as 127.0.0.1/index.php), name1 is the name of the form element, value1 is the value of the form element. The URL and form elements are separated by "?", and multiple form elements are separated by "&". The format of each form element is name=value, which is a fixed format and routine. Just remember it.

Note: To submit a form using the GET() method, the length of the URL should be limited to 1MB characters. If the amount of data sent is too large, the data will be truncated, resulting in unexpected or failed processing results.

Let's create a form to submit the user name and password using the GET() method, and display it in the URL address bar. Add a text box, named user; add a password field, named pwd; set the method attribute of the form to the GET() method, the sample code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>form</title>
</head>
<body>
<form action="index.php" method="get" name="form1">
  <table width="500" border="0" cellpadding="0"  cellspacing="0">
    <tr>
      <td width="500" height="30">
            用户名:<input type="text" name="user" size="12">
            密 码:<input type="password" name="pwd" id="pwd" size="12">
        <input type="submit" name="submit" value="提交">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
Copy after login

Run this example, in the text box Enter the user name and password in the "Submit" button, and the information in the text box will be displayed in the URL address bar, as shown in the following figure:

POST() method and GET() method for submitting form data in php

here It can be clearly found that the GET() method will expose parameters in the address bar. If the parameters passed by the user are non-confidential parameters (such as id=8), then it is feasible to use the

GET() method to pass data; if the parameters passed by the user are confidential parameters (such as passwords, etc.), use Passing data this way is unsafe. The solution to this problem is to replace the default GET() method of the method attribute in the form with the POST() method.

The above is the detailed content of POST() method and GET() method for submitting form data in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!