• 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,改改就能用。

  • 相关阅读:
    解决Android Studio Gradle DSL method not found: 'android()'
    【转】关于ListView中notifyDataSetChanged()刷新数据不更新原因
    设计模式-单例模式
    IE浏览器让DIV居中
    Java通过DOM解析XML
    git 配置文件位置;git配置文件设置
    git config配置
    dos2unix
    文件的编码问题解决
    git diff old mode 100644 new mode 100755
  • 原文地址:https://www.cnblogs.com/kenshinobiy/p/4399030.html
Copyright © 2020-2023  润新知