views:

497

answers:

2

Hi,

I am passing some parameters to my PHP page thru POST request and in the PHP page I am reading it like;

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

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

PS: I am using CodeIgniter PHP framework.

+4  A: 

I googled 'codeigniter input post'.

First result is this http://codeigniter.com/user_guide/libraries/input.html

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 }

Thanks a lot. It worked.
Veera
A: 

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: http://codeigniter.com/user_guide/libraries/form_validation.html

You'd so 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.

Jona