如何将$res从代码点火器模型传递到视图中的 Ajax 函数?



我想从表中验证用户详细信息。我的模型如下所示:

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;
}

这是我想将来自 Model 的结果作为对象传递的地方,然后帮助我指示要加载的页面。我查看的功能是:

$.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();
});

你做的一切都是正确的,但是当你回显truefalse时,它无法被 ajax 响应读取。而不是将$res设置为truefalse,将其设置为10

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;
}

j查询代码 :

成功回调data变量将具有您的$res。无需显式传递它。

如果$res1它将重定向到"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();
});

模型

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();
});

相关内容

最新更新