0

I added

<script type="text/javasript" src="<?=base_url()?>js/jquery-1.7.2.min.js"></script>

in application/views/templatess/header.php and to post data to Controller using jQuery I wrote

<script>

$(document).ready(function(){

    $('#submit').click(function() {
        $.ajax({
            type : 'POST',
            url : '/projects/create',
            data: {
                pro_name : $('#pro_name').val()
            },
            success:function (data) {
                $("#log_msg").html(data);
            }          
        });
    });

});
</script>

in my view file: application/views/project/create.php and the controller exists in application/controllers/projects.php

In my view named create.php the complete code is:

<label for="pro_name">Project Name</label>
<input type="input" id="pro_name" name="pro_name" />
<br />
<input type="submit" id="submit" name="submit" value="Create" />

<script>

$(document).ready(function(){

    $('#submit').click(function() {
        $.ajax({
            type : 'POST',
            url : '/projects/create',
            data: {
                email : $('#pro_name').val()
            },
            success:function (data) {
                $("#log_msg").html(data);
            }          
        });
    });

});
</script> 

and my controller named projects contains

class Projects extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('projects_model');
        $this->load->helper('url');
        $this->load->helper('html');

        $this->load->helper('form');
        $this->load->library('form_validation');
    }

    public function create()
    {
        echo $this->input->post('pro_name');

        $data['title'] = 'SPARCS | Create Project';

        $this->form_validation->set_rules('pro_name', 'Name', 'required');
        $this->form_validation->set_rules('pro_client', 'Client', 'required');
        $this->form_validation->set_rules('pro_loc_city', 'City', 'required');
        $this->form_validation->set_rules('pro_loc_state', 'State', 'required');
        $this->form_validation->set_rules('pro_size', 'Project Size', 'required');      
        $this->form_validation->set_rules('pro_desc', 'Description', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);   
            $this->load->view('projects/create');
            $this->load->view('templates/footer');

        }
        else
        {
            // If project name already exists?

            $query = $this->projects_model->if_exists($this->input->post('pro_name'));

            if ( sizeof($query) == 0) {

                //$this->projects_model->set_project();             
                $data['message'] = 'Add successfully';

            } else {

                $data['message'] = 'Project name already exists';
            }

            $this->load->view('projects/log_message', $data);
        }
    }
}

But browser says $ is not defined

Now please let me know how to setup jQuery in CodeIgniter and what is the correct way to pass data to the controller and also show response send back from the controller

5
  • Where in the browser does it say $ is not defined? Are you sure your jQuery path is correct? Commented May 27, 2012 at 5:01
  • In firebug, yes jquery is located at root/js/jquery.js I saw view source and when clicked and it is correct and if i clicked it show the js file Commented May 27, 2012 at 5:09
  • I recommend that you change the url in your ajax request from /projects/create to <?= site_url('projects/create'); ?> Commented May 27, 2012 at 5:25
  • I also follow stackoverflow.com/questions/5051059/… but same error shows $ is not defined Commented May 27, 2012 at 6:27
  • Okay I fix it we should avoid to use <?= instead we should use <?php echo "your_script_here";?> So I replace <?=base_url()?> with <?php echo base_url();?> Commented May 27, 2012 at 6:36

1 Answer 1

0

Is it possible that somewhere in your code, jQuery is being set into "safe mode"?

Try wrapping your logic in an anonymous function, like this:

​(function($) {
    // insert logic here
})(jQuery)​

Whatever is in this function is guaranteed to work with $ as a shortcut for jQuery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.