我想从表中验证用户详细信息。我的模型如下所示:
public function validate_login(){
$this->db->where(
array(
'login_username' => $this->input->post('username'),
'login_password' =>$this->input->post('password'))
);
$query = $this->db->get('login')->num_rows();
if ($query > 0){
$res = true;
}
else{
$res = false;
}
return $res;
}
在这里,当我尝试回复$ res时,它不会在我的视图中显示任何信息。我的控制器看起来:
function validate_login()
{
$res = $this->bizmanager->validate_login();
echo $res;
}
这是我想将模型中的结果作为对象传递的地方,然后帮我指导要加载的页面。我的观点是:
$.ajax({
type:"post",
url: base_url + 'home/validate_login',//URL changed
data:{
'username':username,
'password':password},
success:function(data){
if (result == true)
{
window.location ="home/home";
}
else{
window.location ="home/load_register";
}
}
});
$('#frmlogin').each(function()
{
this.reset();
});
答案 0 :(得分:2)
您正在做正确的事情,但当您回复true
或false
时,ajax响应无法读取它。不要将$res
设置为true
false
,而是将其设置为1
和0
。
public function validate_login(){
$this->db->where(array(
'login_username' => $this->input->post('username'),
'login_password' =>$this->input->post('password'))
);
$query = $this->db->get('login')->num_rows();
if ($query > 0){
$res = 1;
}
else{
$res = 0;
}
return $res;
}
jQuery代码:
成功回调的 data
变量将包含您的$res
。您不需要明确传递它。
如果$res
为1
,则会重定向到" home / home"否则为" home / load_register"
$.ajax({
type:"post",
url: base_url + 'home/validate_login',//URL changed
data:{
'username':username,
'password':password
},
success:function(data){
if (data === '1') {
window.location ="home/home";
} else {
window.location ="home/load_register";
}
}
});
$('#frmlogin').each(function() {
this.reset();
});
答案 1 :(得分:1)
模型
public function validate_login(){
$this->db->where(
array(
'login_username' => $this->input->post('username'),
'login_password' =>$this->input->post('password'))
);
$query = $this->db->get('login')->num_rows();
if ($query > 0){
$res = 1;
}else{
$res = 0;
}
return $res;
}
您的控制器代码将
function validate_login()
{
$data = $this->bizmanager->validate_login();
return json_encode($data);
}
查看代码
$.ajax({
type:"post",
dataType : "json",
url: base_url + 'home/validate_login',//URL changed
data:{ 'username':username, 'password':password},
success:function(data){
if (data=='1')
{
window.location ="home/home";
}else{
window.location ="home/load_register";
}
}
});
$('#frmlogin').each(function(){
this.reset();
});