2013-07-26 45 views
0

我是CodeIgniter的新手。無法使控制器和查看註冊表一起工作

目前我有一個register.php(VIEW)和一個register.php(CONTROLLER)。 register.php(VIEW)包含一個簡單的表單,從這個表單我試圖傳遞數據到控制器,以便將其插入數據庫。簡單。

每當我加載視圖時,我似乎得到的是與變量和函數有關的不同錯誤消息,也是我嘗試加載視圖的錯誤。

我只是問:

  1. 這是正確的方法是使用控制器和視圖在一起嗎?
  2. 我該如何解決這個當前的問題?

下面是兩個文件:

register.php(VIEW)

<htmL> 

<body> 

<form method="post" action="controllers/register.php"> 

<input type="text" name="email"> 
<input type="text" name="name"> 
<input type="password" name="password"> 
<select id="userLevel" name="userLevel">   
    <option value="2">Job Seeker</option> 
    <option value="3">Employer</option> 
</select> 
<input type="submit" name="submit" value="Submit"> 

</form> 

</body> 

</htmL> 

register.php(控制器)

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Users extends CI_Controller{ 

public function __construct(){ 

    public $email; 
    public $name; 
    public $password; 
    public $userLevel; 

    $this->email = $_POST['email']; 
    $this->name = $_POST['name']; 
    $this->password = $_POST['password']; 
    $this->userLevel = $_POST['userLevel']; 

} 

public function createuser() { 

    if(filter_var($this->email, FILTER_VALIDATE_EMAIL)) { 

     $this->db->set('email', $this->email); 
     $this->db->set('name', $this->name); 
     $this->db->set('password', $this->password); 
     $this->db->set('userLevel', $this->userLevel); 
     $this->db->insert('users'); 

    } 

} 

$this->load->view('register'); 

} 

?> 
+0

你爲什麼要定義類在構造函數中的變量?您可能想打開錯誤報告並查看錯誤日誌中的內容。 –

+0

@tereško我敢肯定,我提到我是一名初學者,所以如果你可以幫忙,而不是問這些問題。 – AyeTry

+0

不,我現在會回答正確的方法 –

回答

3

你的控制器看起來應該是這樣:

class Users extends CI_Controller{ 
public function __construct(){ 
    parent::__construct(); 
} 

public function createuser() { 
    if($this->input->post(null)){ 
     $data = array(); 
     $data['email']  = $this->input->post('email'); 
     $data['name']  = $this->input->post('name'); 
     $data['password'] = $this->input->post('password'); 
     $data['userLevel'] = $this->input->post('userLevel'); 
     $this->db->insert('users', $data); 
    } 
    $this->load->view('register'); 
} 
} 

而不是寫<form method="post" action="controllers/register.php"><?=form_open('user/createuser')?>

+0

插入方法是完全錯誤的 –

+0

@dianuj所以我不應該用這個方法呢? – AyeTry

+0

是的,對@dianuj,我現在編輯了代碼。它應該工作。 –

2

你需要這樣做。這是MVC和更清晰

VIEW

<htmL> 

<body> 

<?php echo form_open('controllers/createuser'); ?> 
<input type="text" name="email"> 
<input type="text" name="name"> 
<input type="password" name="password"> 
<select id="userLevel" name="userLevel">   
    <option value="2">Job Seeker</option> 
    <option value="3">Employer</option> 
</select> 
<input type="submit" name="submit" value="Submit"> 

<?php echo form_close(); ?> 

</body> 

</htmL> 

控制器

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Users extends CI_Controller{ 

    public function __construct(){ 
     $this->load->helper('form'); 

    } 

    public function createuser() { 

     $data['email'] = $this->input->post['email']; 
     $data['name'] = $this->input->post['name']; 
     $data['password'] = $this->input->post['password']; 
     $data['userLevel'] = $this->input->post['userLevel']; 

     if($this->input->post('submit')) { 

      // Here you can validate data if you want 

      $this->user_model->insert($data); 
      redirect('users/success'); 
     } 
     $this->load->view('register'); 
    } 

    function success() { 
     echo "You add user"; 
    } 



} 

    ?> 

型號

class User_model extends CI_model { 

    public function __construct() { 
     parent:: __construct(); 
    } 

    function add($data) { 
     $this->db->insert('users', $data); // table name users 
    } 
    } 
+0

所有顯示的內容都是在將所有代碼插入到相應文件後找不到的頁面。 – AyeTry

+0

'<?php echo form_open('users/createuser'); ?>' –

+0

您需要根據代碼進行調整,因爲我沒有全部結構。我只想用這個答案來展示MVC的方式以及如何添加一個簡單的用戶。所以只需調整你的代碼,檢查任何「;」因爲我沒有測試它,我把它寫在答案textarea –