Welcome Guest, Not a member yet? Register   Sign In
[Solved] $ is not defined
#1

[eluser]babazumbula[/eluser]
I have jQuery script that works fine on page until certain method is called.
Controller:
Code:
function  index(){        
        $data['main_content'] = 'login_form_view';
        $this->load->view('includes/template',$data);          
}

View:
Code:
<?php $this->load->view('includes/header'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer'); ?>

At this point everything works fine, my jQuery script is loaded and works as it should.
Now, calling validate_credentials() from the same controller, causes that jQuery stops working. In firebug console I've got error "$ is not defined", and hence js stops working. I checked whether jquery.js is loaded before calling script, and checked if $(document).ready(function(){}) is taking place, but everything seem just fine.
Code:
function validate_credentials(){
        $this->load->library('form_validation');
        $this->load->model('membership_model');
        $this->form_validation->set_rules('username','Username',
                                          'callback_correct_credentials');
        if($this->form_validation->run() == false){
           $this->index();          
        }
        else{
           // some code...            
        }
}


function correct_credentials(){
        $this->load->model('membership_model');
        if($this->membership_model->validate()){        
            return true;
        }else{
            $this->form_validation->set_message('correct_credentials', 'Incorrect
                                                username/password. Try again') ;
            return false;
         }
}

In model:
Code:
function validate(){
        $this->db->where('username',$this->input->post('username'));
        $this->db->where('password',md5($this->input->post('password')));
        $query = $this->db->get('membership');
        if($query->num_rows() == 1){
            return true;
        }
        else{return false;}
}

Basically function validate_credentials() when executing $this->index() is loading same view again (now with showing login errors), but this time jQuery is not defined, unlike index() is being called by controller at the first place.
#2

[eluser]treeface[/eluser]
It's hard to tell what's going wrong without seeing what's in your jQuery scripts. At the very least, a link to the page you're working on would be helpful and a description of what the javascript should be doing or what tests you're performing to see if jQuery has loaded. Have you looked through the source code on the page to see if the jQuery include is actually occurring? Where are you calling validate_credentials() from?
#3

[eluser]babazumbula[/eluser]
[quote author="treeface" date="1280716436"]Have you looked through the source code on the page to see if the jQuery include is actually occurring? Where are you calling validate_credentials() from?[/quote]
I've looked in source code, and it occurs.
validate_credentials() is called from the same controller as index().
Currently project is only on my localhost, don't have any link yet.
In essence it is ajax voting. User selects radio button as answer and hits "Vote". Results are displayed asynchronously (dynamically from database) as soon as hitting "Vote".
Strange thing is that it works when index() loaded as default controller method, but won't when validate_credentials() calls it.
Script is simple and is as follows:
[code]
$(document).ready(function(){
$("#poll #vote").click(function () {
var val = $('input:radio[name=vote]:checked').val();
if(!val){alert('Please choose one answer');die();}
$("#poll").html('<img src="&lt;?php echo base_url();?&gt;picture/ajax-loader.gif"
#4

[eluser]babazumbula[/eluser]
$.post("&lt;?php echo base_url();?&gt;login/poll/", {"vred" : val},
function(data){$("#poll").html(data);}

);
});
});
[/code]

For some reason I excluded above bits of code. Anyhow, now it's complete.
#5

[eluser]babazumbula[/eluser]
Here is the link with explanation:

Link
#6

[eluser]babazumbula[/eluser]
Solution is to use full path in &lt;head&gt;, so when I put

Code:
&lt;script type="text/javascript" src="&lt;?php echo base_url(); ?&gt;js/jquery.js">&lt;/script&gt;

instead of

Code:
&lt;script type="text/javascript" src="js/jquery.js">&lt;/script&gt;

problem got solved!




Theme © iAndrew 2016 - Forum software by © MyBB