Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将$res从codeigniter模型传递到视图中的ajax函数?_Php_Ajax_Codeigniter - Fatal编程技术网

Php 如何将$res从codeigniter模型传递到视图中的ajax函数?

Php 如何将$res从codeigniter模型传递到视图中的ajax函数?,php,ajax,codeigniter,Php,Ajax,Codeigniter,我想从表中验证用户详细信息。我的模型如下所示: 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'

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

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

您做的一切都是对的,但是当您回显
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代码:

数据
成功回调变量将具有您的
$res
。您不需要显式地传递它

如果
$res
1
它将重定向到“主/主”或“主/加载寄存器”

模型

您的控制器代码将如下所示:

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

非常感谢你的回答。这帮了大忙。我是Codeigniter的新手。现在如何将该值传递给view函数?因此,如果该值不是1,或者该条件是否有效?再次感谢。@MosesMwicigi您不需要显式地传递它。success:function(data),您的数据将在ajaxsuccess的数据变量中可用。请检查我的jquery代码。@MosesMwicigi检查编辑答案,并提供关于我的jquery代码的更多细节。谢谢@Touheed Khan。它解决了我的挑战。我非常感激@莫斯姆维奇吉听到这个消息很高兴。请接受我的回答。
    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();
});