How to pass a variable between views? Codeigniter

Codeigniter noob here:

I want users to click a button inside someone's profile to send them a message, I need to transfer the variable from the view back to the controller and to another view, how can I do this? In the first view, the variable is: $ username:

Preview # 1: (it 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 available in the user / send_message view. Thank you for your help!

+4
source share
2 answers

Change the following line

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

For

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

So your public function user_message($username){ ... } will get the $username parameter as the parameter. Once you get it in the controller method, you can send it to the second view when you load the view with other data, e.g.

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

Then you can use $username in your view.

+2
source

Why not go through the hidden field and send it to your controller. Try it.

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'); } 
0
source

Source: https://habr.com/ru/post/1479831/


All Articles