我有下面的控制器,我希望它做以下事情:
如果使用了用户名则显示错误 - >目前只是提交为 正常甚至通过特定用户在数据库中。
如果出现错误,请填充数据 - >目前填充 导入的数据是否正确
查看:
<h1><?php echo $companyName; echo nbs(1);?> - <?php echo $pageTitle; ?></h1>
<?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';
}
?>
<p>Error: <?php echo validation_errors();?></p>
<div class="formContent">
<form action="createUser" method="post">
<fieldset class="control-group">
<label for="userName">User Name:</label><input type="text" id="userName" name="userName" value="<?php echo set_value('userName'); ?>" placeholder="User Name">
<label for="userPassword">User Password:</label><input type="password" id="userPassword" name="userPassword" value="<?php echo set_value('userPassword'); ?>" placeholder="User Password">
<label for="userFirstName">First Name:</label><input type="text" id="userFirstName" name="userFirstName" value="<?php echo set_value('userFirstName'); ?>" placeholder="First Name">
<label for="userLastName">Last Name:</label><input type="text" id="userLastName" name="userLastName" placeholder="Last Name">
<label for="userEmail">E-Mail:</label> <input type="text" id="userEmail" name="userEmail" placeholder="Admin E-mail">
<label for="userGroup"> User Group:</label>
<select name="userGroup" id="userGroup">
<option value="select">Please Select</option>
<option value="admin">Admin</option>
<option value="user">User</option>
</select>
<label for="userActive"> User Active:</label>
<select name="userActive" id="userActive">
<option value="select">Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div>
<button type="submit" class="btn-primary">Create</button>
</div>
</fieldset>
</form>
</div>
控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class createUser extends CI_Controller {
public function index()
{
//Form Validation prep making sure its all clean
$this->form_validation->set_rules('userName', 'User Name', 'trim|required|is_unique[users.userName]|xss_clean');
$this->form_validation->set_rules('userPassword', 'User Password', 'trim|required|xss_clean|sha1');
$this->form_validation->set_rules('userFirstName', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('userLastName', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('userEmail', 'E-Mail', 'trim|required|xss_clean');
$this->form_validation->set_rules('userGroup', 'User Group', 'trim|required|xss_clean');
$this->form_validation->set_rules('userActive', 'User Active', 'trim|required|xss_clean');
//If form validation fails load previous page with errors else do the job and insert data into db
if($this->form_validation->run() == FALSE)
{
$data['success'] = "";
}else{
$username = $this->input->post('userName');
$password = $this->input->post('userPassword');
$firstname = $this->input->post('userFirstName');
$lastname = $this->input->post('userLastName');
$email = $this->input->post('userEmail');
$group = $this->input->post('userGroup');
$active = $this->input->post('userActive');
$passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1? MD5 is for tossers
// If the data is correct follow through with db insert
if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$group,$active))
{
$data['success'] = TRUE;
}
}
$data['companyName'] = $this->core_model->companyName();
$data['pageTitle'] = "Create User";
$this->load->view('admin/assets/header', $data);
$this->load->view('admin/createUser', $data);
$this->load->view('admin/assets/footer');
}
}
/* End of file login.php */
/* Location: ./application/controllers/admin/createUser.php */
答案 0 :(得分:1)
如果我是你,我会使用回调(http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks)
$this->form_validation->set_rules('userName', 'User Name', 'trim|required|xss_clean|callback__checkUsername');
对于您的选择下拉菜单,请使用set_value()函数
<select name="userGroup" id="userGroup">
<option value="select"<?=(set_value('userGroup')=='select')?' selected="selected"':''?>>Please Select</option>
<option value="admin"<?=(set_value('userGroup')=='admin')?' selected="selected"':''?>>Admin</option>
<option value="user"<?=(set_value('userGroup')=='user')?' selected="selected"':''?>>User</option>
</select>
祝你好运
答案 1 :(得分:0)
我纯粹是从一个逻辑的PHP点开始,因为我对codeigniter一无所知 关于检查用户名是否已被占用,我会在“用户”表中的用户名字段上进行数据库查询,看看该名称是否存在。
我看到你这样做了:
$this->form_validation->set_rules('userName', 'User Name', 'trim|required|is_unique[users.userName]|xss_clean');
我不知道在igniter中引用db表的标准,但是你可能错误地引用了这些字段。此外,我会在检查时将每个更改为小写,以确保您检查不区分大小写...除非您想要区分大小写的用户名。