Veera
Veera

Reputation: 33202

Missing Parameter in PHP $_POST Function

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

$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?

Upvotes: 0

Views: 1532

Answers (2)

Jona
Jona

Reputation: 305

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.

Upvotes: 0

mozboz
mozboz

Reputation: 1119

I Googled 'codeigniter input post'.

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.

So you need to do:

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

Upvotes: 4

Related Questions