如何在ajax提供的控制器中检索文本框值?

时间:2015-03-04 05:56:37

标签: php ajax codeigniter

我想将文本框的值传递给我的控制器,我可以通过但我无法在控制器中检索它。帮我识别我的错误。

Form.php(dept_name是我的文本框的id)

<script>   
                    $(document).ready(function() {
                        $("#submitbutton").click(function(e) { 
                            var deptname=$('#dept_name').val();                           
                            $.ajax({
                                url: 'http://localhost/finalProjectWork/admin_department_controller/adddept/',
                                type: 'POST',
                                method: 'POST',
                                headers: {'Cache-Control': 'no-cache'},
                                data: {name:deptname},
                                contentType: false,
                                processData: false,
                                success: function(test) {
                                    alert(test);

                                },
                                error: function() {
                                    alert("Already Exists");
                                }
                            });
                            e.preventDefault(); 
                        });
                    });</script>

控制器:

 public function adddept()
    {
        $deptname=$this->input->post('name');

        $this->load->model('admin_department_model');
        $this->admin_department_model->insertdept($deptname);
        echo "Successfully inserted";
    }

1 个答案:

答案 0 :(得分:0)

试试这个

<script>   
                    $(document).ready(function() {
                        $("#submitbutton").click(function(e) { 
                            var deptname=$('#dept_name').val();                           
                            $.ajax({
                                url: 'http://localhost/finalProjectWork/admin_department_controller/adddept/',
                                type: 'POST',
                                method: 'POST',
                                headers: {'Cache-Control': 'no-cache'},
                                data: {name:deptname},
                                success: function(test) {
                                    alert(test);

                                },
                                error: function() {
                                    alert("Already Exists");
                                }
                            });
                            e.preventDefault(); 
                        });
                    });</script>