Home > PHP Framework > ThinkPHP > Commonly used methods to submit forms in thinkphp

Commonly used methods to submit forms in thinkphp

Release: 2020-04-16 09:16:28
forward
4807 people have browsed it

Commonly used methods to submit forms in thinkphp

Commonly used form submission operations in ThinkPHP include the post method and the get method.

The post method is safer than the get method. If you use the get method, your submitted form, including account password and other information, will be displayed in the access path, while the post method will hide its data.

Rewrite the add method to determine whether it is a post request. If so, process the form submission. If not, display the template.

Extension: How to determine whether the request is a post?

Answer: We can use if($_POST) to judge, but in ThinkPHP the system encapsulates several commonly used constants for us. We can directly use constants to judge. Common constants are as follows:

IS_POST If the request is post, the value of IS_POST is true, otherwise it is false

IS_GET

IS_AJAX If the request is ajax, the value of IS_AJAX is true, otherwise it is false

IS_CGI

IS_PUT

Instructions on data reception:

When we used $_POST to receive data before, In ThinkPHP, we can use the I method (quick method) to receive data. The I method can receive any type of input (post, get, request, put, etc.), and the system comes with a method to prevent SQL injection by default (using PHP's built-in function htmlspecialchars).

Commonly used methods to submit forms in thinkphp

The variable type is similar to get, post, etc.

The variable name refers to the subscript of the specific element in $_GET or $_POST.

Default value: If the original content becomes an empty string after using the filtering method, the default value will be used instead.

Filtering method: It is a supplement to the htmlspecialchars provided by ThinkPHP by default. The function name can be built-in in PHP or in the function library.

Extra instructions: What if you want to receive the entire array?

If you want to receive all the data, you don’t need to write the variable name, you can write it as I(‘get.’);

<?php      
  public function add(){
          if(IS_POST){
            //处理表单提交
            $post = I(&#39;post.&#39;);
            //添加数据
            //实例化模型
            $model = M(&#39;Dept&#39;);
            $result = $model -> add($post);
            //判断返回值
            if($result){
              //添加成功
              $this -> success(&#39;添加成功&#39;,U(&#39;showList&#39;),5);
            }else{
              $this -> error(&#39;添加失败&#39;);
            }
          }else{
          //实例化模型
          $model = M(&#39;Dept&#39;);
          //查询操作
          $data = $model -> where(&#39;pid = 0&#39;) -> select();
          //变量分配
          $this -> assign(&#39;data&#39;,$data);
          //展示模板
          $this -> display();
        }
}
Copy after login

Recommended tutorial: thinkphp tutorial

The above is the detailed content of Commonly used methods to submit forms in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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