1

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.

1 Answer 1

3

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; ?>">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.