CodeIgniter典型的表单提交验证代码

view内容:

<?php echo form_open('user/reg'); ?>

    <h5>用户名</h5>

    <input type="text" name="username" value="<?php echo $this->validation->username;?>" size="50" />

    <span id="error_msg"><?php echo $this->validation->username_error; ?></span>



    <h5>密码</h5>

    <input type="password" name="password"  size="50" />

    <span id="error_msg"><?php echo $this->validation->password_error; ?></span>



    <h5>确认密码</h5>

    <input type="password" name="passconf"  size="50" />

    <span id="error_msg"><?php echo $this->validation->passconf_error; ?></span>



    <h5>Email</h5>

    <input type="text" name="email" value="<?php echo $this->validation->email;?>" size="50" />

    <span id="error_msg"><?php echo $this->validation->email_error; ?></span>

    <div><input type="submit" value="Submit" /></div>



    </form>

 

controller内容:

$this->load->library('validation');

$this->load->helper('form');



$rules['username'] = "trim|required|min_length[5]|max_length[16]|xss_clean|callback_username_check";

$rules['password'] = "trim|required|matches[passconf]|md5";

$rules['passconf'] = "trim|required";

$rules['email'] = "trim|required|valid_email|callback_email_check";

$this->validation->set_rules($rules);



$fields['username'] = '用户名';

$fields['password'] = '密码';

$fields['passconf'] = '密码确认';

$fields['email'] = '邮箱地址';

$this->validation->set_fields($fields);



if ($this->validation->run() == FALSE)

{

    $this->load->view('user/reg_view');

}

else

{

    //调用这个函数保存数据到数据库

    $this->user_model->reg();

    $this->load->view('user/reg_suc_view');

}

 

model内容:

$activation_key = md5(uniqid() + time());

$data = array

(

    'username' => $this->input->post('username'),

    'password' => $this->input->post('password'),

    'email' => $this->input->post('email'),

    'registered_date' => time(),

    'activation_key' => $activation_key,

    'status'=>0,

    'level'=>0,

    'reserve_times'=>0,

    'credit'=>0,

);



$this->db->insert($this->config->item('db_prefix').'user', $data);

碰到类似的表单提交的功能,直接ctrl+c、ctrl+v,改改就能用。

你可能感兴趣的:(CodeIgniter)