You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将HTML表格数据提交至CodeIgniter控制器的PHP方法

提交静态表单字段+动态表格数据到CodeIgniter控制器

要把表单里的患者信息和动态添加的表格测试数据一起提交到CodeIgniter控制器,需要从表单结构调整、动态行的表单元素设置、控制器数据接收三个环节处理:

1. 调整动态表格的行结构

因为表格行是JS动态生成的,必须把每行的测试数据包装成表单可识别的提交字段。给每个数据项设置数组格式的name属性,这样提交后控制器能以数组形式接收所有行数据。

示例动态添加行的JS代码:

// 动态添加测试行的函数(根据你的实际逻辑修改参数)
function addTestRow(testId, testName, price) {
    const tbody = document.querySelector('#dest_table tbody');
    const row = document.createElement('tr');
    
    // 用隐藏input存储每个测试项的参数,name采用数组格式tests[][xxx]
    row.innerHTML = `
        <td>${testId}<input type="hidden" name="tests[][id]" value="${testId}"></td>
        <td>${testName}<input type="hidden" name="tests[][name]" value="${testName}"></td>
        <td>${price}<input type="hidden" name="tests[][price]" value="${price}"></td>
        <td style="text-align:center;">
            <button type="button" class="btn btn-danger btn-sm" onclick="this.closest('tr').remove()">删除</button>
        </td>
    `;
    
    tbody.appendChild(row);
}

如果需要让用户编辑价格等字段,直接把隐藏input改成可见的表单控件即可,比如:

<td><input type="number" class="form-control" name="tests[][price]" value="${price}"></td>

2. 完善表单的提交配置

给表单指定提交的CodeIgniter控制器方法,同时确保CSRF令牌(如果开启了CI的CSRF保护):

<!-- 用CI的form_open自动生成CSRF令牌,或者手动添加 -->
<form method="POST" action="<?php echo base_url('your_controller/submit_patient_tests'); ?>">
    <!-- 手动添加CSRF令牌(如果不用form_open) -->
    <?php echo form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()); ?>
    
    <!-- 原有的患者信息字段 -->
    <input type="text" class="form-control" id="pname" placeholder="Enter Patient name here" name="pname">
    <input type="number" class="form-control" id="p-age" name="p-age">
    <input type="radio" id="male" name="sex" value="male">
    <label for="male" class="receipt-label mr-2">Male</label>
    <input type="radio" id="female" name="sex" value="female">
    <label for="female" class="receipt-label mr-2">Female</label>
    
    <!-- 原有的表格 -->
    <table class="table" id="dest_table">
        <!-- 表格thead和tbody保持不变 -->
    </table>
    
    <button type="submit" name="submit" class="btn btn-info btn-lg" id="submit">Print</button>
</form>

替换your_controller为你的实际控制器类名,submit_patient_tests为处理提交的方法名。

3. 在CodeIgniter控制器中接收并处理数据

在控制器方法里,通过$this->input->post()获取静态字段和动态表格的数组数据,还可以做数据验证:

<?php
class Your_controller extends CI_Controller {
    public function submit_patient_tests() {
        // 获取患者基本信息
        $patient_info = [
            'name' => $this->input->post('pname'),
            'age' => $this->input->post('p-age'),
            'sex' => $this->input->post('sex')
        ];
        
        // 获取动态表格的测试数据数组
        $tests_list = $this->input->post('tests');
        
        // 数据验证(可选但推荐)
        $this->load->library('form_validation');
        
        // 验证基本字段
        $this->form_validation->set_rules('pname', 'Patient Name', 'required|trim');
        $this->form_validation->set_rules('p-age', 'Patient Age', 'required|numeric');
        $this->form_validation->set_rules('sex', 'Sex', 'required');
        
        // 验证每个测试项
        if (!empty($tests_list)) {
            foreach ($tests_list as $index => $test) {
                $this->form_validation->set_rules("tests[$index][id]", 'Test ID', 'required|numeric');
                $this->form_validation->set_rules("tests[$index][name]", 'Test Name', 'required|trim');
                $this->form_validation->set_rules("tests[$index][price]", 'Price', 'required|numeric');
            }
        }
        
        if ($this->form_validation->run() === TRUE) {
            // 验证通过,执行后续操作(比如存入数据库)
            // 示例:保存患者信息
            // $this->db->insert('patients', $patient_info);
            // 保存测试数据
            // foreach ($tests_list as $test) {
            //     $test['patient_id'] = $this->db->insert_id();
            //     $this->db->insert('patient_tests', $test);
            // }
            
            echo "数据提交成功";
            // 也可以跳转到结果页面
            // redirect('your_controller/success_page');
        } else {
            // 验证失败,返回错误信息
            echo validation_errors();
            // 或者返回表单页面并保留输入
            // $this->load->view('your_form_view');
        }
    }
}
?>

关键注意点

  • 确保CodeIgniter的base_url配置正确,否则表单的action会指向错误地址。
  • 如果动态行允许用户删除,删除操作不需要额外处理,因为删除后对应的input元素也会被移除,不会提交无效数据。
  • 前端可以添加提交前的验证,比如检查是否至少添加了一行测试数据,避免空的测试数组提交。

内容的提问来源于stack exchange,提问作者muizz alvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 18:20:27