Backend Development 4 min read

Handling Form Submissions in ThinkPHP: POST vs GET and Using the I Method

This article explains how to handle form submissions in ThinkPHP by distinguishing POST and GET requests, using built‑in constants to detect request types, and employing the I method for secure data retrieval, illustrated with a complete add() controller example.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Handling Form Submissions in ThinkPHP: POST vs GET and Using the I Method

In ThinkPHP, common form submission methods include the POST and GET methods.

The POST method is more secure because GET parameters appear in the URL, potentially exposing sensitive information such as usernames and passwords.

To rewrite the add method, first check whether the request is a POST; if it is, process the form submission, otherwise display the template.

ThinkPHP provides several constants for request type detection, such as IS_POST (true when the request is POST), IS_GET , IS_AJAX , IS_CGI , IS_PUT , and others.

For receiving data, instead of using $_POST directly, ThinkPHP offers the I() helper method, which can retrieve input from any source (post, get, request, put, etc.) and automatically applies htmlspecialchars to mitigate SQL injection.

The I() method takes a variable type (e.g., 'post.' ) and a variable name; if the filtered value becomes an empty string, a default value can be supplied.

To receive an entire array of inputs, omit the variable name, for example I('get.') will return all GET parameters as an array.

Below is a complete PHP example demonstrating these concepts:

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

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.