Welcome Guest, Not a member yet? Register   Sign In
View files doesnt get variable data
#1

[eluser]Carmichael[/eluser]
Hi i am working with a site using CodeIgniter. I have one static template where I include head, sidebar and foot and then I the dynamic template wich is the controller ex Regisiter, News etc.

The sidebar is a view file that gets data from the Widget and Login controller. Every widget has its own view file. The problem is when I include the head file the widget view files doesnt get variable data from its controller.
#2

[eluser]Carmichael[/eluser]
Example #1


Code:
function login()
{
  $data['title'] = "Login";
  
  if ($this->session->userdata('logged_in') == true)
  {
   redirect('/');
  }
  
  // validation form input
  $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
  $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
  
  if ($this->form_validation->run() == TRUE)
  {
   $username = $this->input->post('username');
   $password = $this->input->post('password');
  
   $result = $this->Auth_model->login($username, $password);
  
   if ($result == TRUE)
   {
    // login...
    $data['message'] = $this->session->set_flashdata('message', 'Du är nu inloggad!');
    
    redirect('/');
   }
   else
   {
    // login fail...
    $data['message'] = $this->session->set_flashdata('message', 'Felaktiga uppgifter!');
    
    redirect('login');
   }
  }
  else
  {
   // display the login user form
   // and set flashdata to show errors if there's
  
   $data['message'] = (validation_errors()) ? validation_errors() : '';
  
   $data['username'] = array(
    'type'    => 'text',
    'name'    => 'username',
    'id'    => 'username'
   );
  
   $data['password'] = array(
    'type'    => 'password',
    'name'    => 'password',
    'id'    => 'password'
   );
  
   $this->load->view('login', $data);  
  }

}

Login view
Code:
<?php echo form_open('login') ?>
    
    <h1>Logga in</h1>
    
    &lt;?php echo $message; ?&gt;
    
    <p><label for="username"><b>Användarnamn:</b></label>
    &lt;?php echo form_input($username); ?&gt;</p>
    
    <p><label for="password"><b>Lösenord:</b></label>
    &lt;?php echo form_input($password); ?&gt;</p>
    
    <p>&lt;?php echo form_submit('login', 'Logga in'); ?&gt;</p>
    
    &lt;?php echo form_close(); ?&gt;

When I include the login view file in the main page the variables $username and $password aren't valid.

main.php
Code:
$this->load->view('login');
#3

[eluser]Samus[/eluser]
try print_r() those variables?

I normally pass those values from within my view, rather than the controller.

also you don't need

'type' => 'text' or 'type' => 'password'

form_input() is used for normal text input and form_password is used for password fields.
#4

[eluser]InsiteFX[/eluser]
Try this:
Code:
$this->load->vars($data);
$this->load->view('login');
#5

[eluser]Carmichael[/eluser]
Still same problem. Don't get variable data from the controller. :/

If I turn to the question. How do you guys include header, sidebar and footer?
#6

[eluser]InsiteFX[/eluser]
template.php
Code:
&lt;?php this->load->view('header');
&lt;?php this->load->view('content', $data);
&lt;?php this->load->view('footer');




Theme © iAndrew 2016 - Forum software by © MyBB