我使用jquery验证插件。我一点也不知道远程功能是怎么工作的。
到目前为止,这是我的代码:functions.js
user: {
required: true,
remote: {
url: "http://localhost/iTransaction/home/checkUser",
type: "post",
async: false
}
}
控制器:
function checkUser(){
return $this->itransaction_model->checkUser();
}
模型:
function checkUser(){
$this->db->select("user");
$query = $this->db->get("member");
$row = $query->row();
echo $row->user;
if($row->user == $this->input->post("user")){
return true;
}else{
return false;
}
}
远程检查用户名是否已经存在
From the docs:
服务器端资源通过jQuery调用。ajax (XMLHttpRequest)并获取一个键/值对,该键/值对对应于已验证元素的名称及其作为GET参数的值。响应作为JSON进行评估,对于有效元素必须为true,对于无效元素可以为任何false、undefined或null,使用默认消息;或者字符串。"这个名字已经有人用了,试试peter123代替"作为错误信息显示。
你的控制器方法以一种对库没有任何意义的方式返回一个布尔值,并被解释为false。您需要以另一种方式返回验证结果:
function checkUser(){
$this->output->set_content_type('application/json');
$this->output->set_header('Cache-Control: no-cache, must-revalidate');
$this->output->set_header('Expires: '.date('r', time()+(86400*365)));
$output = json_encode(
$this->itransaction_model->checkUser() ? true : 'That username is alreay in use'
);
$this->output->set_output($output);
}