繁体   English   中英

使用ajax函数在codeigniter中重定向

[英]redirect in codeigniter using ajax function

这是我的控制器功能

    public function verifyUser()    
   {
        $userName =  $_POST['email'];
        $userPassword =  $_POST['password'];
        $var=array('email'=>$userName,'password'=>$userPassword);
        $check=$this->mymodel->login_validation($var);


        //$status = array("STATUS"=>"false");
        if(count($check))
        {
            redirect('main/valid_login');

        }
        else
        {
        echo "<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>Could't Authorize to the system! Try again with valid credentials.</div>" ;
        }   
    }

这是我的ajax功能

    <script>
    function makeAjaxCall(){
    $.ajax({
    type: "post",
    url: "<?php echo site_url('main/verifyUser');?>",
    cache: false,               
    data: $('#userForm').serialize(),
     success:function(msg)
    {
        $('#show_id').html(msg);
    }
    });
   }
   </script>

这是我的表格

 <form name="userForm" id="userForm" action="">
    <div id="show_id"></div>
            <fieldset>

                <p><label for="email">E-mail address</label></p>
                <p><input type="email" id="email" placeholder="enter your email id" name="email"></p> <!-- JS because of IE support; better: placeholder="mail@address.com" -->

                <p><label for="password">Password</label></p>
                <p><input type="password" id="password" placeholder="*******" name="password" style="width: 328px;"></p> <!-- JS because of IE support; better: placeholder="password" -->

                <p><input type="button" value="Sign In" onclick="javascript:makeAjaxCall();"></p>

            </fieldset>

        </form>

一切正常,但是当我输入有效的用户名和密码时,它没有重定向到任何页面,因此请帮助我

将JSON数据发送到ajax作为响应,并根据需要进行处理。
Contorller:

public function verifyUser()  {

    $userName =  $_POST['email'];
    $userPassword =  $_POST['password'];
    $var=array('email'=>$userName,'password'=>$userPassword);
    $check=$this->mymodel->login_validation($var);


   //$status = array("STATUS"=>"false");
   if(count($check)) {
        $this->output
            ->set_content_type("application/json")
            ->set_output(json_encode(array('status'=>true, 'redirect'=>base_url('main/valid_login') )));
   }
   else {
        $this->output
            ->set_content_type("application/json")
            ->set_output(json_encode(array('status'=>false, 'error'=>'Could't Authorize to the system! Try again with valid credentials.')));
    }   
}

使用ajax处理JSON数据。

$.ajax({
    type: "post",
    url: "<?php echo site_url('main/verifyUser');?>",
    cache: false,               
    data: $('#userForm').serialize(), 
    dataType: 'json', 
    success:function(response) {
        if( response.status === true )
            document.location.href = response.redirect;
        else 
            $('#show_id').html("<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>"+response.error+"</div>");
    }
});

控制器功能:

public function verifyUser()   
    {
    $userName =  $_POST['email'];
    $userPassword =  $_POST['password'];
    $var=array('email'=>$userName,'password'=>$userPassword);
    $check=$this->mymodel->login_validation($var);


    //$status = array("STATUS"=>"false");
    if(count($check))
    {
        echo 1;

    }
    else
    {
    echo "<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>Could't Authorize to the system! Try again with valid credentials.</div>" ;
    }   
}

Ajax功能

    <script>
        function makeAjaxCall(){
        $.ajax({
        type: "post",
        url: "<?php echo site_url('main/verifyUser');?>",
        cache: false,               
        data: $('#userForm').serialize(),
         success:function(msg)
        {
         if(msg== "1")
         {
             //redirect here
          }
            $('#show_id').html(msg);
        }
        });
       }
       </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM