1

I have a controller where I have data being sent either via GET or POST either one at anytime but not both, because of this i get error

Message: Undefined index: userid

this is either due to POST or GET depends on what one is not found.

How can i add a statement so that if no POST is found look for GET and not have the error displayed ?

MY CODE

      public function index()
    {

        $this->session->userdata('user_id');
        $userID = $this->session->userdata('userid');

//        var_dump($userID);
        $this->session->set_userdata('user_id', $_POST['userid']);
        $this->session->set_userdata('user_id', $_GET['userid']);

        $data['tests'] = simplexml_load_file("tests/tests.xml");
        $this->load->view('selecttest',$data);
    }

}
Benjamin Oats
  • 573
  • 1
  • 8
  • 25
  • 1
    Check reference for post_get input helper https://www.codeigniter.com/user_guide/libraries/input.html#CI_Input::post_get – Rishi Dec 01 '16 at 12:46

4 Answers4

0

Use $_REQUEST

http://php.net/manual/en/reserved.variables.request.php

just check for the variable in $_REQUEST superglobal

Since you are using CI you can also use $this->input->post_get('userid', TRUE);

Rishi
  • 162
  • 1
  • 9
0

Change this :

    $this->session->set_userdata('user_id', $_POST['userid']);
    $this->session->set_userdata('user_id', $_GET['userid']);

To :

$this->session->set_userdata(
    'user_id',
    (
        $this->input->get('userid') == null ? 
        $this->input->post('userid') : 
        $this->input->get('userid')
    )
);
aprogrammer
  • 1,764
  • 1
  • 10
  • 20
0

You can try with Ternary Operator.

$user_id = isset($_GET['userid']) && ($_GET['userid']!= '') ? $_GET['userid'] : (isset($_POST['userid']) && ($_POST['userid']!='') ? $_POST['userid'] : null);
$this->session->set_userdata('user_id', $user_id);

Quick Links

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

You can achieve this by doing something of this sort.

if($this->input->post('userid')){
 $userid=$this->input->post('userid');
}
else{
$userid=$this->input->get('userid');
}

Refer this link for more details : https://www.codeigniter.com/user_guide/libraries/input.html

5eeker
  • 1,016
  • 1
  • 9
  • 30