CodeIgniter Forums

Full Version: AJAX and jQuery problems at passing information to a CI Controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Isern Palaus[/eluser]
Hello,

I recently started coding AJAX (today) and I've decided to use jQuery. I don't use the library for CI because it had some troubles trying to configure it and it's easiest to talk of the jQuery JS files.

I'm trying to do a simple login (when it will be useful I'll write a few lines about to people who start coding AJAX) that checks the username/password. The problem is that it checks and EVERY TIME if it's correct or not returns me a "yes" on success. Here is the code:

The View One:
Code:
<?php
    $this->load->view('baseHeader');
?>

<div id="contentlogin">
    &lt;?= form_open('user/login',array('id' => 'login')); ?&gt;
        <label for="username">Your Username:</label>
        &lt;input type="text" id="username" name="username" value="" class="text" /&gt;<br />
        <label for="password">Password:</label>
        &lt;input type="password" id="password" name="password" value="" class="text" /&gt;<br />
        <label></label>
        &lt;input type="submit" name="entrar" id="entrar" value="Login" /&gt;<br />
    &lt;?= form_close(); ?&gt;
</div>

<div id="login_msg">

</div>

&lt;?php
    $this->load->view('baseFooter');
?&gt;
Note: the baseHeader view includes the necessary jQuery files and the user.js I'm using.

user.js
Code:
$(document).ready(
    function(){
    
        $("#login").ajaxForm({
            type: "POST",
            url: "http://localhost/dalia/trunk/public_html/index.php/user/checkLogin",
            dataType: "JSON",
            data: "username="+$("#username").val()+"&password;="+$("#password").val(),
            success: function(data){
                $("#contentlogin").slideUp("slow",
                    function() {
                        // This effect it's not working too! :-(
                        $("#login_msg").fadeIn("slow",
                            function() {
                                $(this).html("Test");
                            }
                        );
                    }
                );
                
                // This IFs doesn't works... :S!
                if(data.success == "yes") {
                    $("#login_msg").html("Esto ha validado");
                }
                
                if(data.success == "no") {
                    $("#login_msg").html("Esto NO ha validado");
                }
            }
        });
    
    }
)

The if(data.success... doesn't works too, anybody knows why?

The part of the controller that contains the function checkLogin:
Code:
function _check_login($username) {
        $password = md5($this->validation->password);
        
        $this->user_model->checkUserLogin($username,$password,"users");
    }

    function checkLogin()
    {
        $this->load->library('validation');
        $rules['username'] = 'trim|required|callback__check_login';
        $rules['password'] = 'trim|required';
        $this->validation->set_rules($rules);
        
        $fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        
        if ($this->validation->run()) {
            $output = "{success: 'yes', message: 'This is working'}";
        } else {
            $output = "{success: 'no', message: 'This isn't working'}";
        }
        
        $output = str_replace("\r", "", $output);
        $output = str_replace("\n", "", $output);
        
        echo $output;
    }

The part of the model that checks the user login:
Code:
function checkUserLogin($username,$password,$table=users)
    {
        $query = $this->db->where("username",$username);
        $query = $this->db->where("password",$password);
        $query = $this->db->limit(1,0);
        $query = $this->db->get($table);
        
        if ($query->num_rows() == 0) {
            $this->validation->set_message('_check_login', 'Invalid login');
            return FALSE;
        } else {
            return TRUE;
        }
    }

Well, thanks in advance to everyone.

Regards,
-- Isern Palaus

El Forum

[eluser]Isern Palaus[/eluser]
Hello,

One of the mistakes on de jQuery was that writting JSON in uppercase it doesn't gets as OBJECT:
Code:
dataType: "JSON",

Change it for:
Code:
dataType: "json",

And now YES :-).

When I have the all AJAX code/controller/model I'll publish as a Tutorial.

Regards,
-- Isern Palaus