4

Codeigniter noob here:

I am wanting to let users click a button inside someone's profile in order to send them a message, I need to pass the variable from the view back to the controller and into another view, how can I accomplish this? The variable is $username in the first view:

View #1: (this works)

 <a href="<?php echo base_url().'user/user_message';?>">
       <button type="submit" class="btn btn-info btn-small"  title="Send Message" >Send Message</button>
         </a>

 <h3><?php echo $username;?>- Public Profile</h3>

Controller:

 public function user_message($username)
{
    if($this->form_validation->run() == FALSE)
     {
         $this->load->view('header_loggedin');
         $this->load->view('user/send_message', $username);
         $this->load->view('footer');
     }
     else

I basically want to grab the $username variable from my first view and make it avaliable in the user/send_message view. Thanks for your help!

1
  • Essentially what Sheikh is saying is that you can pass any vars to whatever views you choose from your controller. You're already loading it in your controller. Might as well use the system the way it is designed. Technically you COULD include the second view from within the first, but that would be outside the standard use of CI so it's just not a good idea Commented May 9, 2013 at 0:40

2 Answers 2

2

Change the following line

<a href="<?php echo base_url().'user/user_message';?>">

To

<?php echo base_url().'user/user_message/'.$username;?>

So, your public function user_message($username){ ... } will receive the $username as it's parameter. Once you get it in your controller method then you can send it to the second view when you load the view with other data, like for example,

...
$data['username'] = $username;
$this->load->view('viewname', $data);

Then you can use $username in your view.

Sign up to request clarification or add additional context in comments.

3 Comments

You have to pass the variable to the second view from your controller.
how can I pass a variable not via URI, like a long html string? from view -> controller -> another view ?
Clearify your question please @alex?
0

Why not pass hidden field and post it to your controller. Try this.

VIEW

<a href="<?php echo base_url().'user/user_message';?>">
<button type="submit" class="btn btn-info btn-small"  title="Send Message" >Send  
Message</button>
</a>
<input type="hidden" name="username" value="<?php echo $username; ?>"/>

CONTROLLER

public function user_message()
{
     $username = $this->input->post('username');  
     if($this->form_validation->run() == FALSE)
     {
         $this->load->view('header_loggedin');
         $this->load->view('user/send_message', $username);
         $this->load->view('footer');
     }

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.