Home  >  Article  >  Backend Development  >  How to get data in form through PHP post method? (code example)

How to get data in form through PHP post method? (code example)

藏色散人
藏色散人Original
2018-08-27 15:17:3215449browse

In the process of website development, we usually encounter related operations on php form forms. For example, if PHP obtains form data with post submission method, how to operate it? If you have read my [How does PHP obtain form form data through the get method? 】In this article, I believe everyone already has a certain understanding of the method of php $_GET[] variable to obtain form data.

Then this article will continue to give you a detailed introduction to how PHP obtains form form data through the post method.

There are specific code examples below:

1. Form form code (post method submission form example):

<html>
<head>
    <meta charset="utf-8">
    <title>form表单post方法示例</title>
</head>
<body>
<form action="/test/test.php" method="post" >
    <form action="test.php" method="post">
        名字: <input type="text" name="fname"><br>
        年龄: <input type="text" name="age"><br>
        <input type="submit" value="提交">
    </form>
</form>
</body>
</html>

Access through browser, the effect is As shown below:

How to get data in form through PHP post method? (code example)

As shown above, we enter the name and age and click submit to the test.php file.

2. The test.php code is as follows (php gets post data):

<?php
header("content-type:text/html;charset=utf-8");         //设置编码
?>
欢迎 <?php echo $_POST["fname"]; ?>!<br>
你的年龄是 <?php echo $_POST["age"]; ?>  岁。

Here we get the form through the PHP $_POST[] variable The form data is the fname and age parameters (recommended to refer to Chapter 2 of PHP Zero Basics Introductory Tutorial: PHP Form). After submission, the browser access result is as follows:

How to get data in form through PHP post method? (code example)

You can notice here that no form parameters appear in the link in the browser address bar. That is to say, the form information submitted by PHP obtained through the post method is not displayed at all in the address bar. In web form design, most people use the post submission method because it is much more secure than the get method for submitting forms, can avoid the leakage of sensitive information, and there is no limit to the amount of information sent by post. But since the variable does not appear in the URL, the page cannot be bookmarked.

The above content is about the specific method and function of PHP to obtain post submission form data. It has certain reference value and I hope it will be helpful to friends in need.

The above is the detailed content of How to get data in form through PHP post method? (code example). 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

Related articles

See more