Welcome Guest, Not a member yet? Register   Sign In
how to make var_dump variables visible to inside controller
#1

[eluser]adityajoshi[/eluser]
I am having problem how to use the value of var_dump from login page in the controller

My login page passes the following variable to controller but i am not still able to use it when i try to use inside the controller

my controller is

<?php
$result = $_POST[‘username’];
var_dump($_POST);
class Login extends Controller {
public function __construct() {
parent::Controller();

$this->load->helper(array(‘form’,‘url’));
$this->load->library(‘form_validation’);
$this->load->library(‘session’);
}

public function index() {
$this->load->view(’/’);
}

public function submit() {

if ($this->_submit_validate() === FALSE) {
$this->session->set_userdata(array(‘userid’=>$result));
$sess = $this->session->userdata(‘userid’);
echo $sess;
$this->index();
return;
}
$this->session->set_userdata(array(‘userid’=>$result));


redirect(‘http://localhost/calendar/add’);

}

i am getting error


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: controllers/login.php

Line Number: 24

even if i try to just mere echo $result in side my controller it doesn’t recognise it


Any help will be highly appreciated
#2

[eluser]bproctor[/eluser]
Since $result is used outside of the class it's a global variable. When you try to access it inside your class, it thinks you want to create a new local variable called $result. To access it inside your class, you'll need to tell it it's a global variable with this:

Code:
global $result;

Then you can echo it or whatever inside your controller

Code:
public function index() {
   global $result;
   echo $result;
}
#3

[eluser]adityajoshi[/eluser]
it doesn't work when i try this

Global $val= $_POST['username'];

error
Parse error: syntax error, unexpected '=', expecting ',' or ';'

when i just echo $_POST['username']; it echo the result
#4

[eluser]Bart v B[/eluser]
that = is wrong:
Take a look at my example Tongue
Code:
<?php

class Test extends Controller
{    
    function __construct()
    {
        parent::__construct();
        global  $var;
        
    }
    public function index()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST' )
        {
            
            $var = var_dump($_POST['username']);
        }
        
        $this->load->view('test');
    }
}
#5

[eluser]adityajoshi[/eluser]
reply many thanks it works
#6

[eluser]adityajoshi[/eluser]
I am still having problem

public function __construct() {
parent::Controller();
global $var;
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$this->load->library('session');
}

public function index() {
$this->load->view('/');

}

public function submit() {
$var = var_dump($_POST['username']);
$this->session->set_userdata(array('userid'=> $this->var));
$sess = $this->session->userdata('userid');
if ($this->_submit_validate() === FALSE) {
echo $sess;
$this->index();
return;
}

redirect('http://localhost/calendar/add');

}

when i go to calendar add it wont show me the value of userid
but if I assign

public function submit() {
$var = var_dump($_POST['username']);
$this->session->set_userdata(array('userid'=> "testing"));
$sess = $this->session->userdata('userid');
if ($this->_submit_validate() === FALSE) {
echo $sess;
$this->index();
return;
}
it will show me the value of userid in this page ('http://localhost/calendar/add');
this code is in my view page
<?php
$sess = $this->session->userdata('userid');
echo $sess; ?>
#7

[eluser]Bart v B[/eluser]
Instead of using $this->index() a redirect()?

So the code would be like this:

Code:
public function submit() {
//$var = var_dump($_POST[‘username’]);
//this->session->set_userdata(array(‘userid’=> “testing”));
//$sess = $this->session->userdata(‘userid’);
  if ($this->_submit_validate() === FALSE)
  {
     // to validate i think?
     $this->index();
  }
  else
  {
    $var = var_dump($_POST[‘username’]);
    $this->session->set_userdata(array(‘userid’=> 'testing'));
  
  }
  redirect('calender/add');
  exit();
}
#8

[eluser]adityajoshi[/eluser]
it works this time
#9

[eluser]adityajoshi[/eluser]
Thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB