Question

I'm new to CodeIgniter.

At the moment I have a register.php(VIEW) and a register.php(CONTROLLER). The register.php(VIEW) contains a simple form and from this form I am trying to pass data to the controller in order to insert it to the database. Simple.

Whenever I load the view all I seem to be getting is different error messages in relation to the variables and functions, also errors on the line which I try to load the view.

I'm simply asking:

  1. Is this the correct way to be using controllers and views together?
  2. How can I fix this current problem?

Below are the two files:

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 (CONTROLLER)

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

}

?>

No correct solution

OTHER TIPS

Your controller should look like this:

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

Instead of writing <form method="post" action="controllers/register.php"> write <?=form_open('user/createuser')?>

You need to do like this. It is MVC and more clear

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>

Controller

    <?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";
    }



}

    ?>

Model

class User_model extends CI_model {

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

    function add($data) {
        $this->db->insert('users', $data);  // table name users
    }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top