2013-07-26 46 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 –