0

我添加了

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

application/views/templatess/header.php并使用我写的 jQuery将数据发布到Controller

<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>

在我的视图文件中:application/views/project/create.php并且控制器存在于application/controllers/projects.php

在我看来,名为create.php的完整代码是:

<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> 

我的控制器名为项目包含

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);
        }
    }
}

但是浏览器说$ is not defined

现在请让我知道如何在CodeIgniter中设置 jQuery,以及将数据传递给控制器​​并显示从控制器发回的响应的正确方法是什么

4

1 回答 1

0

是否有可能在您的代码中某处将 jQuery 设置为“安全模式”?

尝试将您的逻辑包装在匿名函数中,如下所示:

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

保证此函数中的任何内容都可以$用作jQuery.

于 2012-05-27T05:19:31.910 回答