Nayantara Jeyaraj Nayantara Jeyaraj - 24 days ago 6
PHP Question

Pass Variable from View to Controller in Codeigniter using PHP

I am working on a project where I need to pass a variable from the View to a Controller's method where I'll be using that variable's value. I have tried the following.

View

...
$user = 3;
...
<ul class="nav navbar-nav navbar-right">
<li>
<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=$user">
My Dashboard
</a>
</li>
...


studentDashboardController (Method 1)

public function index()
{
...
if ( isset($_GET['user']) ) {
$user = $_GET['user'];
echo '<script type="text/javascript">alert("User taken from GET: ' . $user . '")</script>';
}
...


Output for Method 1

Output for Method 1

studentDashboardController (Method 2)

public function index()
{
...
if($this->input->get())
{
$user = $this->input->get('user');
echo '<script type="text/javascript">alert("Uid taken from Method 2 ' . $user . '")</script>';
}
...


Output for Method 2

Output for Method 2

Any suggestions on how to get the value of this passed variable will be highly appreciated.

Answer

You were missing the PHP notation while printing the $user variable. Update the below line in the View

<a href="<?php echo base_url() ?>index.php/studentDashboardController/index?user=<?php echo $user; ?>">