2011-10-11 16 views
0

目前我在考慮登錄腳本。想法 - 當您提交登錄名和密碼時,他們會檢查模型,如果登錄名和密碼正確,腳本會顯示有關完成登錄過程的消息。但是,如果我放入視圖echo $message;並且我在登錄頁面,kohana顯示錯誤 - 未定義的變量$消息。但是,當我在頁面上,變量定義,所有工作。如何實現這個想法。有關登錄腳本的想法

編輯!

class Controller_About extends Controller_Template { 

public function action_index() { 
    session_start(); 
    if (isset($_SESSION['lietotajvards'])) { 

     if (!empty($_POST['virsraksts']) and !empty($_POST['saturs'])) { 

      $name = Model::factory('index')->insert_names($_POST['virsraksts'], $_POST['saturs']); 
      $result = $name; 
      $this->template->site_name = Kohana::$config->load('common')->get('site_name'); 
      $this->template->site_description = Kohana::$config->load('common')->get('site_description'); 
      $this->template->page_title = 'About'; 
      $this->template->content = View::factory('about/about')->set('result', $result); 
      $this->template->styles[] = 'index/index'; 
     } else { 
      $this->template->site_name = Kohana::$config->load('common')->get('site_name'); 
      $this->template->site_description = Kohana::$config->load('common')->get('site_description'); 
      $this->template->page_title = 'About'; 
      $this->template->content = View::factory('about/about'); 
      $this->template->styles[] = 'index/index'; 
     } 
    } else { 
     if (!empty($_POST['lietotajvards']) and !empty($_POST['parole'])) { 
      $user_model = Model::factory('index')->valide($_POST['lietotajvards'], md5($_POST['parole'])); 
      foreach ($user_model as $d) { 
       if ($_POST['lietotajvards'] == $d['lietotajvards'] and md5($_POST['parole']) == $d['parole']) { 
        $_SESSION['lietotajvards'] = $_POST['lietotajvards']; 
        $this->template->content = View::factory('login/index')->set('message', 'Ielogošanās veiksmīga!'); 
        echo '<meta http-equiv="refresh" content="5" url="http://127.0.0.1/about">'; 
       } 
      } 
     } 
     $this->template->site_name = Kohana::$config->load('common')->get('site_name'); 
     $this->template->site_description = Kohana::$config->load('common')->get('site_description'); 
     $this->template->page_title = 'About'; 
     $this->template->content = View::factory('login/index'); 
     $this->template->styles[] = 'index/index'; 
    } 
}} 

我只有變量$消息的問題。

<?php echo $message; ?> 
<form action="" method="post"> 
    <input type="text" name="lietotajvards" id="" /> 
    <input type="password" name="parole" id="" /> 
    <input type="submit" value="Nosūtīt datus!" /> 
</form> 
+1

1.爲什麼要顯示消息而不重定向/顯示主頁? 2.向我們展示您遇到問題的代碼 – matino

+0

您可以顯示「動作」,「模型」和「視圖」嗎? – vietean

+0

請檢查我的編輯。 – reGative

回答

1

問題是你在最後設置模板內容,這是重寫你之前設置的內容。嘗試改變

$this->template->content = View::factory('login/index')->set('message', 'Ielogošanās veiksmīga!'); 

$message = 'Ielogošanās veiksmīga!'; 

,然後在端更改視圖裝載到

$this->template->content = View::factory('login/index')->bind('message',$message); 

使用視圖上的綁定方法將空值傳遞給視圖如果該變量未在控制器內設置。