PHP $ _POST函数中缺少参数

时间:2022-03-24 01:57:16

I am using CodeIgniter to pass some parameters to my PHP page through $_POST request, and in the PHP page I am reading.

我正在使用CodeIgniter通过$ _POST请求将一些参数传递给我的PHP页面,并在我正在阅读的PHP页面中。

$foo = $this->input->post('myParam');

If the myParam parameter is present in the $_POST request, then $foo will be assigned the myParam value. How do I check if myParam is not passed in the $_POST request?

如果$ _POST请求中存在myParam参数,则会为$ foo分配myParam值。如何检查$ _POST请求中是否未传递myParam?

2 个解决方案

#1


I Googled 'codeigniter input post'.

我用谷歌搜索'codeigniter输入帖子'。

First result is this.

第一个结果就是这个。

From that document:

从该文件:

$this->input->post('some_data');

The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.

如果您尝试检索的项目不存在,则该函数返回FALSE(布尔值)。

So you need to do:

所以你需要这样做:

if ($foo===false) {
   // do something if it's not set
}

#2


I think the best way to do this would be to use the form validation class to do pre-processing on your data. This is documented here.

我认为最好的方法是使用表单验证类对数据进行预处理。这在此处记录。

You'd do something like:

你会做类似的事情:

function index()
{
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('myParam', 'myParam', 'required');
            if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}

If your validation fails it will send you back to the form and you'll have to re-populate it with data, there is a way to do this (see the doc). If it passes you can then be sure that $this->input->post('myParam'); will return a value.

如果您的验证失败,它会将您发回给表单,您必须使用数据重新填充它,有一种方法可以执行此操作(请参阅文档)。如果它通过你可以确定$ this-> input-> post('myParam');将返回一个值。

#1


I Googled 'codeigniter input post'.

我用谷歌搜索'codeigniter输入帖子'。

First result is this.

第一个结果就是这个。

From that document:

从该文件:

$this->input->post('some_data');

The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.

如果您尝试检索的项目不存在,则该函数返回FALSE(布尔值)。

So you need to do:

所以你需要这样做:

if ($foo===false) {
   // do something if it's not set
}

#2


I think the best way to do this would be to use the form validation class to do pre-processing on your data. This is documented here.

我认为最好的方法是使用表单验证类对数据进行预处理。这在此处记录。

You'd do something like:

你会做类似的事情:

function index()
{
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('myParam', 'myParam', 'required');
            if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}

If your validation fails it will send you back to the form and you'll have to re-populate it with data, there is a way to do this (see the doc). If it passes you can then be sure that $this->input->post('myParam'); will return a value.

如果您的验证失败,它会将您发回给表单,您必须使用数据重新填充它,有一种方法可以执行此操作(请参阅文档)。如果它通过你可以确定$ this-> input-> post('myParam');将返回一个值。