Json 如何使用ajax在Codeigniter控制器中获取表单数据?

Json 如何使用ajax在Codeigniter控制器中获取表单数据?,json,ajax,forms,codeigniter,Json,Ajax,Forms,Codeigniter,大家好,请访问上面提到的链接。 无论何时从下拉列表中选择名称,都会列出与该特定名称相关的所有数据 在循环数据中,我在tr和submit按钮周围添加了表单,并添加了一些附加字段,如考勤、贷款、利率等。每当为特定人员点击submit按钮时。它应该存储在我的数据库表中,该表称为工资表 现在我的问题是当我们点击提交按钮时,无法获取模型中的数据 如果您检查它,您可以看到数组正在那里创建,但没有来自提交表单的数据 请参阅下面的图片链接 //看法 <?php include "template/hea

大家好,请访问上面提到的链接。 无论何时从下拉列表中选择名称,都会列出与该特定名称相关的所有数据

在循环数据中,我在tr和submit按钮周围添加了表单,并添加了一些附加字段,如考勤、贷款、利率等。每当为特定人员点击submit按钮时。它应该存储在我的数据库表中,该表称为工资表

现在我的问题是当我们点击提交按钮时,无法获取模型中的数据

如果您检查它,您可以看到数组正在那里创建,但没有来自提交表单的数据 请参阅下面的图片链接

//看法

<?php include "template/header.php"; ?><!-- Start Page content -->

    <div class="content">
        <div class="container-fluid">
            <div class="card-box">
                <form action="%3C?php%20echo%20base_url('con_employee/employeeSearch');%20?%3E" class="form-inline" method="post">
                    <div class="form-group" style="padding-right:10px; width:100%;">
                        <select class="custom-select" id="empDesignation" name="empDesignation">
                            <option selected>
                                Designation
                            </option><?php foreach($categories as $category){ ?>
                            <option value="<?php echo $category['cat_id']; ?>">
                                <?php echo $category['cat_name']; ?>
                            </option><?php } ?>
                        </select>
                    </div>
                </form><br>
                <div class="row">
                    <table class="table table-hover table-centered m-0 table-bordered">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Designation</th>
                                <th>Account Number</th>
                                <th>Attendance</th>
                                <th>Rate</th>
                                <th>Loan</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody id="salaryData"></tbody>
                    </table>
                </div>
            </div>
        </div><!-- container -->
    </div><!-- content -->
    <!-- end row -->
    <script>
    $(function() {


       $('#empDesignation').change(function() {
           var user_designation = $(this).val();
           if (user_designation == '') {
               $('#empName').prop('disabled', true);
           } else {
               $('#empName').prop('disabled', false);
               $.ajax({
                   url: "<?php echo site_url(); ?>con_salary/add_salary_for_employee",
                   type: "POST",
                   data: {
                       'user_designation': user_designation
                   },
                   dataType: 'json',
                   success: function(data) {

                       var html = '';
                       var i;
                       for (i = 0; i < data.length; i++) {
                           html += '<tr>' +
                               '<form class="" id="myform">' +
                               '<td>' + data[i].emp_name + ' <\/td>' +
                               '<td>' + data[i].cat_name + '<\/td>' +
                               '<td>' + data[i].emp_account_number + '<\/td>' +
                               '<td>' +
                               '<input type="text" class="form-control"  name="attendance" placeholder="Attendance" required>' +
                               '<\/td>' +
                               '<td>1<\/td>' +
                               '<td>' +
                               '<input type="text" class="form-control"  name="loan" placeholder="Loan" required>' +
                               '<\/td>' +
                               '<td>' +
                               '<a href="javascript:;" class="btn btn-primary item-edit" id="btnSave">Submit<\/a>' +
                               '<\/td>' +
                               '<\/form>' +
                               '<\/tr>';
                       }
                       $('#salaryData').html(html);

                       // $('#salaryData').html(data);
                   },
                   error: function() {
                       alert('No Data');
                   }

               });

           }
       });
    });


    //insert data to salary table
       $('#salaryData').on('click', '.item-edit', function() {
           $('#myform').attr('action', '<?php echo base_url() ?>con_salary/addSalary');

           var url = $('#myform').attr('action');
           var data = $('#myform').serialize();
           var attendance = $('input[name=attendance]');
           var loan = $('input[name=loan]');    


               $.ajax({
                   type: 'ajax',
                   method: 'POST',
                   url: url,
                   data: data,
                   async: false,
                   dataType: 'json',
                   success: function(response) {

                   },
                   error: function() {

                   }
               });

       });


    </script> <?php include "template/footer.php"; ?>

任命

名称 任命 帐号 出勤 比率 贷款 行动 $(函数(){ $('#empsignation')。更改(函数(){ var user_designation=$(this.val(); 如果(用户名称=“”){ $('empName').prop('disabled',true); }否则{ $('empName').prop('disabled',false); $.ajax({ url:“更改薪资/为员工添加薪资”, 类型:“POST”, 数据:{ “用户指定”:用户指定 }, 数据类型:“json”, 成功:功能(数据){ var html=''; var i; 对于(i=0;i
//控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Con_salary extends CI_Controller 
{

public function __construct(){
    parent:: __construct(); 


    $this->load->model('queries_salary');       
}


public function index()
{   $data['categories'] = $this->queries_salary->fetchCategory();
    $this->load->view('employee_salary_view',$data);
}


public function add_salary_for_employee()
{
   $designation_id=$this->input->post('user_designation');
    $users = $this->queries_salary->fetchEmployeeforsalary($designation_id);

    echo json_encode($users);


}

public function addSalary()
{
    $result = $this->queries_salary->addSalary();


    // $msg['success']=false;
    // $msg['type']='add';

    // if($result){
    //     $msg['success']=true;
    // }

    // echo json_encode($msg);

}


//end
}
?>

//模型

<?php 

class Queries_salary extends CI_Model
{

    public function fetchCategory()
    {
        $query= $this->db->get('category');
        return $query->result_array();
    }

    public function fetchEmployeeforsalary($designation_id)
{
    $this->db->where('emp_designation_id',$designation_id);
    $this->db->join('category','employee.emp_designation_id = category.cat_id');
    $query=$this->db->get('employee');


    if($query->num_rows()>0){
        return $query->result();
    }
    else{
        return false;
    }

    // $this->db->where('emp_designation_id',$designation_id);
    // $query=$this->db->get('employee');


}

public function addSalary()
{
    $field = array(
        'salary_attendance'=>$this->input->post('attendance'),
        'salary_loan'=>$this->input->post('loan')
    ); 


    print_r($field);
    exit();

    // $this->db->insert('salary',$field );
    // if($this->db->affected_rows()>0)
    // {
    //     return true;
    // }
    // else{
    //     return false;
    // }



}



//end
}?>

表单不允许是表、tbody或tr HTML元素的子元素。 如果试图放置表单,则会导致浏览器将表单移动到它出现在表之后的位置。因此,在您的情况下,表单会产生空数据

在这里,我调整了你的代码一点,将为你工作

for (i = 0; i < data.length; i++) {
   html += '<tr class="user_data" >' +
       '<td>' + data[i].emp_name + ' <\/td>' +
       '<td>' + data[i].cat_name + '<\/td>' +
       '<td>' + data[i].emp_account_number + '<\/td>' +
       '<td>' +
       '<input type="text" class="form-control attendance"  name="attendance" placeholder="Attendance" required>' +
       '<\/td>' +
       '<td>1<\/td>' +
       '<td>' +
       '<input type="text" class="form-control loan"  name="loan" placeholder="Loan" required>' +
       '<\/td>' +
       '<td>' +
       '<a href="javascript:;" class="btn btn-primary item-edit" id="btnSave">Submit<\/a>' +
       '<\/td>' +                       
       '<\/tr>';
}
for(i=0;i
底端脚本

$('#salaryData').on('click', '.item-edit', function() {

   var url = '<?php echo base_url() ?>con_salary/add_salary_for_employee';
   var current_row = $(this).closest('.user_data');                   
   var data = { attendance: current_row.find('.attendance').val(), loan: current_row.find('.loan').val() };

   $.ajax({
       type: 'ajax',
       method: 'POST',
       url: url,
       data: data,
       async: false,
       dataType: 'json',
       success: function(response) {

       },
       error: function() {

       }
   });

});
$('salaryData')。在('click','上。项编辑',函数(){
var url='con_salary/add_salary_for_employee';
var current_row=$(this).closest('.user_data');
var data={attention:current_row.find('.attention').val(),loan:current_row.find('.loan').val();
$.ajax({
键入:“ajax”,
方法:“POST”,
url:url,
数据:数据,
async:false,
数据类型:“json”,
成功:功能(响应){
},
错误:函数(){
}
});
});
一个重要的注意事项是,如果您使用的是单个表单,并且使用的输入类型相同且名称相同,请将其用作数组


例如:
name=“loan[]”

提交申请表时,申请表的数据是什么?表单是否在那里正确序列化?(如果使用chrome,只需转到dev tools中的标题以获取请求,并一直滚动到底部)感谢您的帮助。我使用了此脚本,但没有发生任何事情,甚至操作都不起作用。我应该把表单的动作标签放在哪里?在脚本或html?中?。以及不触发控制器。