Welcome Guest, Not a member yet? Register   Sign In
jquery ajax login in codeigniter.....
#1

[eluser]sivams20[/eluser]
I have a simple jquery-ajax login form in codeigniter.But its not working.It not even shows error.can anyone help please.

Here is my ajax code

Code:
[removed]

    $(document).ready(function(){

        $("#submit").click(function(){

           var username=$("#usr").val();
           var password=$("#pass").val();

           $.ajax({
            type: "POST",
            url: "/site/logval",
            data: ({usr: username, pass: password}),

            success: function(response){
               if(response=='TRUE')
                {
                    Redirect('site/home');
                }
                else
                {
                   Redirect('site/login');
                }
            }          
        });          
        });  
    });

[removed]


Here is my code for controller
Code:
function login()
        {
            $this->load->view('login');
        }
      
        function logval()
        {
            $this->form_validation->set_rules('usr','Username','required');
            $this->form_validation->set_rules('pass','Password','required');
    
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('login');
            }
            else
            {
                $data['usr'] = $this->input->post('usr');
                $data['pass'] = $this->input->post('pass');
                
              if($this->user_model->check_login($data)== TRUE)
                {
                //$this->load->view('home');
                  return TRUE;
                }
                else
                {
                 //$this->load->view('failure');  
                  return FALSE;  
                }
            }
        
        }

and here is my code for model

Code:
function check_login($data)
    {

        $username=$data['usr'];
        $password=$data['pass'];

            $query = $this->db->select('uname, pass');
            $query = $this->db->from('users');
            $query = $this->db->where('uname', $username);
            $query = $this->db->where('pass', $password);
//            $query = $this->db->where('Password_VC', $password);
            $query = $this->db->get();

            if($query->num_rows() > 0)
            {
                return true;
                //return $query->result();
            }
            else
            {
                return false;
            }
        }

}
#2

[eluser]CodeIgniteMe[/eluser]
First, you need to add the CI's site_url() into the jquery-ajax's url field.
Code:
<$cript>
//contains the base URL of your installation
//e.g : http://yoursite.com/ci/
var base_url = '&lt;?php echo base_url();?&gt;';
//contains the base_url() and your front script(index.php)
//e.g : http://yoursite.com/ci/index.php
var site_url = '&lt;?php echo site_url();?&gt;';
$(document).ready(function(){

        $("#submit").click(function(){

           var username=$("#usr").val();
           var password=$("#pass").val();

           $.ajax({
            type: "POST",
            url: site_url+"/site/logval",
            data: ({usr: username, pass: password}),

            success: function(response){
               if(response=='TRUE')
                {
                    Redirect('site/home');
                }
                else
                {
                   Redirect('site/login');
                }
            }          
        });          
        });  
    });
</$cript>
#3

[eluser]CodeIgniteMe[/eluser]
Next, Append the site_url variable to your redirect so that it redirects correctly
#4

[eluser]sivams20[/eluser]
I have changed the code as you said.But still its not working and shows no error...
#5

[eluser]CodeIgniteMe[/eluser]
Do you have Firebug installed? It's a great tool if you plan developing with ajax. It's an add-on for firefox. Install Firebug, then monitor the console tab. You'll see the output there.
#6

[eluser]CodeIgniteMe[/eluser]
Can you paste the new codes here so we can verify the effectiveness of your new codes?
#7

[eluser]CodeIgniteMe[/eluser]
Oh, now I see why there is no error displayed for your page. It's because you've used Redirect. Redirecting your page unsets the $_POST values to an empty array along with all validations rules and errors. I suggest you use jQuery more extensively, return data via JSON, XML or even a simple Text data to inform javascript of your backend status and messages, then append the message to your HTML output.

Can you please send us a code snippet of your view so we could continue with this?
#8

[eluser]sivams20[/eluser]
Here is my code for view.....

Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;My Registration Home.....&lt;/title&gt;
        
        &lt;link href="&lt;?php echo base_url().'css/style.css' ; ?&gt;" rel="stylesheet" type="text/css" /&gt;
        &lt;link href="&lt;?php echo base_url().'css/form.css' ?&gt;" rel="stylesheet" type="text/css"/&gt;
            
      
          $(document).ready(function()
           {

            $(".sign_in").click(function()
             {
                 $("#sign_box").toggle('slow');
              return false;
              });
          });


    $(document).ready(function(){
        
        $("#submit").click(function(){
            
            var base_url = '&lt;?php echo base_url();?&gt;';
            var site_url = '&lt;?php echo site_url();?&gt;';
            var username=$("#usr").val();
            var password=$("#pass").val();

           $.ajax({
            type: "POST",
            url: "/site/logval",
            data: ({usr: username, pass: password}),
                            
            success: function(response){
               if(response== TRUE)
                {
                    var new_url=site_url+"/site/home";
                    Redirect(new_url);
                }
                else
                {
                    var new_url=site_url+"/site/login";
                   Redirect(new_url);
                }
            }          
        });          
        });  
    });
    



      &lt;!--   &lt;link href="&lt;?php echo base_url().'css/style.css' ; ?&gt;" rel="stylesheet" type="text/css"/&gt; --&gt;
    &lt;/head&gt;
    
    &lt;body&gt;

<menu id="navoption">
                
                        <a href="[removed] void(0);" class="sign_in">LOGIN</a>
                        <div id="sign_box">
                      
                           &lt;?php echo form_open('');?&gt;
                           <p>
                           <label>UserName
                               &lt;input type="text" name="usr" id="usr"/&gt;&lt;/label>
                        &lt;?php echo form_error('usr');?&gt;
                           </p>
                           <p>
                               <label>Password
                               &lt;input type='password' name="pass" id="pass"/&gt;&lt;/label>
                        &lt;?php echo form_error('pass'); ?&gt;
                           </p>
                           &lt;input type="button" id="submit" name="submit" value=" Sing In "/&gt;
                           &lt;/form&gt;
                       </div>
                        
                    </menu>



&lt;/body&gt;
&lt;/html&gt;
#9

[eluser]CodeIgniteMe[/eluser]
Error messages will not be displayed yet if your AJAX code is as simple as this.

First, remove the line that returns false in your controller, so the entire HTML will be outputted from the server-side and is received by AJAX. Second, do not return true from your controller because AJAX/Javascript cannot read boolean from other language (such as PHP). Echo it instead, then exit to terminate the script. Your controller should look more like this
Code:
if($this->user_model->check_login($data)== TRUE)
                {
                  echo 'success';
                  exit();
                //$this->load->view('home');
                }
                else
                {
                 $this->load->view('failure');  
                  //return FALSE;  
                }
Third, since the entire HTML is returned to AJAX if the login is incorrect, do this condition
Code:
success: function(response){
               if(response=='success')
                {
                alert('success');
                }
                else
                {
                $('form').prepend(response);
                }

try it so you'll know if it will work




Theme © iAndrew 2016 - Forum software by © MyBB