Codeigniter $this->input->post() empty while $_POST is working correctly

前端 未结 13 1362
我在风中等你
我在风中等你 2020-12-05 14:56

On a codeigniter installation, I am trying to use the inbuilt $this->input->post(\'some_data\') function, however $this->input->post()

13条回答
  •  我在风中等你
    2020-12-05 15:19

    The problem is that $this->input->post() does not support getting all POST data, only specific data, for example $this->input->post('my_post_var'). This is why var_dump($this->input->post()); is empty.

    A few solutions, stick to $_POST for retrieving all POST data, or assign variables from each POST item that you need, for example:

    // variables will be false if not post data exists
    $var_1 = $this->input->post('my_post_var_1');
    $var_2 = $this->input->post('my_post_var_2');
    $var_3 = $this->input->post('my_post_var_3');
    

    However, since the above code is not very DRY, I like to do the following:

    if(!empty($_POST)) 
    {
        // get all post data in one nice array
        foreach ($_POST as $key => $value) 
        {
            $insert[$key] = $value;
        }
    }
    else
    {
        // user hasen't submitted anything yet!
    }
    

提交回复
热议问题