Home  >  Article  >  Backend Development  >  How to implement form form in PHP

How to implement form form in PHP

王林
王林forward
2019-08-29 16:22:085286browse

1. Basic use of forms

There is a tag in HTML specifically for submitting data: ff9c23ada1bcecdd1a0fb5d5a0f18437, through which user input can be easily collected.

The form tag has two necessary attributes:
action: form submission address (to whom should it be submitted after filling it out)
method: how to submit the form

For example, We need to collect the user name and password entered by the user on the login interface:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF‐8">
  <title>登录</title>
</head>
<body>
  <form action="login.php" method="post">
    <div>
      <label for="username">用户名</label>
      <input type="text" id="username" name="username">
    </div>
    <div>
      <label for="password">密码</label>
      <input type="password" id="password" name="password">
    </div>
    <button type="submit">登录</button>
  </form>
</body>
</html>

According to the current situation, the user requests this form page for the first time, fills in the form content, clicks login, and the form will be automatically sent to login .php, the remaining problem is to consider how to obtain the content submitted by the user in login.php.

There are three super-global variables in PHP specifically used to obtain form submission content:
$_GET : Used to obtain content submitted by GET method
$_POST: Used to obtain content submitted by POST method
$_REQUEST: Used to obtain content submitted by GET or POST method

With the help of $_POST or $_REQUEST, you can get the content submitted by the form:

<?php
// 获取表单提交的用户名和密码
echo &#39;用户名:&#39; . $_REQUEST[&#39;username&#39;];
echo &#39;密码:&#39; . $_REQUEST[&#39;password&#39;];

1.1. Submission address

action The submission address refers to filling in this form After completion, click Submit to determine the request address for sending the request.
From the perspective of ease of maintenance, generally we most commonly submit it to the current file, and then determine whether it is a form submission request in the current file:

<?php
if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
  // 表单提交请求
  }

In addition, it is recommended to use $_SERVER ['PHP_SELF'] Dynamically obtain the current page access path, so that there is no need to modify the code due to file renaming or website directory structure adjustment:

<!‐‐ 这样写死 action 地址,当文件重命名就需要修改代码 ‐‐>
<form action="/foo/login.php">
  <!‐‐ ... ‐‐>
</form>
<!‐‐ 通过 `$_SERVER[&#39;PHP_SELF&#39;]` 获取路径,可以轻松避免这个问题 ‐‐>
<form action="<?php echo $_SERVER[&#39;PHP_SELF&#39;]; ?>">
  <!‐‐ ... ‐‐>
</form>

1.2. Submission method

method can be used to set the form submission method. Currently, we know the two most common form submission methods: GET and POST.
From the effect point of view, both can submit data to the server, but the two are very different in terms of the principle of implementation:
GET
Form data is passed through the ? parameter in the URL
passed to the server You can see the submitted content in the address bar
The data length is limited because the URL address length is limited (2000 characters)
POST
The form data is passed to the request body On the server side, we can’t see it on the interface
You can submit any type of data, including files
Since it’s not visible on the interface and it’s not stored in the browser, it’s safer
As for which one should be used under what circumstances This method needs to be decided based on the business scenario and the respective characteristics of the two methods. There is no absolute answer, only some principles can be given:
Never use GET to send passwords or other sensitive information! ! !
You should think clearly whether this request is mainly to get something or to send something

2. Common form element processing

As for the text in the form element Elements such as box text fields directly use the name attribute value of the element as the key, and the information filled in by the user as the value, and are sent to the server. However, there are some special form elements that need to be considered separately:

2.1 Radio button

<!‐‐ 最终只会提交选中的那一项的 value ‐‐>
<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female">

2.2. Check button

<!‐‐ 没有设置 value 的 checkbox 选中提交的 value 是 on ‐‐>
<input type="checkbox" name="agree">
<!‐‐ 设置了 value 的 checkbox 选中提交的是 value 值 ‐‐>
<input type="checkbox" name="agree" value="true">

If you need to submit multiple selected items at the same time, you can follow the name attribute with []:

https://www.php.net /manual/zh/faq.html.php#faq.html.arrays

<input type="checkbox" name="funs[]" id="" value="football">
<input type="checkbox" name="funs[]" id="" value="basketball">
<input type="checkbox" name="funs[]" id="" value="world peace">

Finally submitted to the server, what is received through $_POST is an index array.

2.3. Selection box

<select name="subject"> 
<!‐‐ 设置 value 提交 value ‐‐>  
<option value="1">语文</option>  
<!‐‐ 没有设置 value 提交 innerText ‐‐>  
<option>数学</option>
</select>

2.4 File upload

The input element whose type attribute is file can be passed through the form Submit a file (upload a file), and the server-side PHP can obtain the uploaded file information through $_FILES.

<?php
// 如果选择了文件 $_FILES[&#39;file&#39;][&#39;error&#39;] => 0
// 详细的错误码说明:http://php.net/manual/zh/features.file‐upload.errors.php
if ($_FILES[&#39;file&#39;][&#39;error&#39;] === 0) {
  // PHP 在会自动接收客户端上传的文件到一个临时的目录
  $temp_file = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];
  // 我们只需要把文件保存到我们指定上传目录
  $target_file = &#39;../static/uploads/&#39; . $_FILES[&#39;file&#39;][&#39;name&#39;];
  if (move_uploaded_file($temp_file, $target_file)) {
    $image_file = &#39;/static/uploads/&#39; . $_FILES[&#39;file&#39;][&#39;name&#39;];
  }
}

$_FILES is also an associative array, the key is the name of the form, the content is as follows:

array(1) {
  ["avatar"]=>
  array(5) {
    ["name"]=>
    string(17) "demo.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(27) "C:\Windows\Temp\php786C.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(29501)
  }
}

For more related questions, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of How to implement form form in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:PHP common command lineNext article:PHP common command line