0

CodeIgniter は初めてです。

現時点では、register.php(VIEW) と register.php(CONTROLLER) があります。register.php(VIEW) には単純なフォームが含まれており、このフォームからデータをコントローラーに渡してデータベースに挿入しようとしています。単純。

ビューをロードするたびに、変数と関数に関連するさまざまなエラーメッセージと、ビューをロードしようとした行のエラーだけが表示されます。

私は単に尋ねています:

  1. これは、コントローラーとビューを一緒に使用する正しい方法ですか?
  2. この現在の問題を解決するにはどうすればよいですか?

以下に 2 つのファイルを示します。

register.php (ビュー)

<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');

}

?>
4

2 に答える 2

2

このようにする必要があります。それはMVCであり、より明確です

見る

<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
    }
    }
于 2013-07-26T10:55:44.987 に答える