【问题标题】:how do pass $res from codeigniter model to my ajax function in view?如何将 $res 从 codeigniter 模型传递给我的 ajax 函数?
【发布时间】:2017-10-19 08:09:39
【问题描述】:

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

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

【问题讨论】:

    标签: php ajax codeigniter


    【解决方案1】:

    您所做的一切都是正确的,但是当您回显 truefalse 时,ajax 响应无法读取它。不要将$res 设置为true false,而是将其设置为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;
    }
    

    jQuery 代码:

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

    【讨论】:

    • 非常感谢您的回答。它有很大帮助。我是 Codeigniter 的新手。现在我如何将该值传递给视图函数?因此,如果值不是 1 或者条件是否有效?再次感谢您。
    • @MosesMwicigi 你不需要明确地传递它。成功:函数(数据),您的数据将在 ajax 成功的数据变量中可用。请检查我的 jquery 代码。
    • @MosesMwicigi 检查编辑答案,并详细说明我的 jquery 代码在做什么。
    • 谢谢@Touheed Khan。它解决了我的挑战。非常感谢!
    • @MosesMwicigi 很高兴听到那个伙伴。请接受我的回答。
    【解决方案2】:

    型号

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

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多