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

如何将表格行的patient_id传递到Bootstrap Modal表单?

动态传递患者ID到Bootstrap模态框表单的解决方案

问题背景

我有一个主表格页面admissions.php,通过数据库查询生成患者入院记录表格,每行包含操作按钮组。当前遇到的问题是:点击表格行内的"Add Note"按钮加载Bootstrap模态框时,无法动态获取当前行的patient_id,只能显示某一条固定记录的ID。

表格结构示例:

Patient_ID | Patient | Presenting complaint
-------------------------------------------
        23 | Dave    | Injured wing
       231 | Steve   | Broken Leg

需求是:打开模态框时,将当前行的admission_patient_id传入模态框内的表单(表单位于add_carenote.php),确保表单提交对应正确的患者记录,且无需离开admissions.php页面。尝试过部分方案导致页面失效或模态框无法加载,想了解合适的实现方式,比如基于表单POST的方案。


方案一:复用单个模态框(通过JavaScript动态传递ID)

这种方案只需要一个模态框,通过点击按钮时把当前行的patient_id赋值给模态框里的隐藏字段,适合不想生成多个模态框的场景。

1. 修改admissions.php中的按钮代码

在触发模态框的按钮上添加data-patient-id属性存储当前行的patient_id,并统一指向同一个模态框ID:

// 循环内的按钮部分修改为:
<button type="button" class="btn btn-primary add-note-btn" data-toggle="modal" data-target="#carenotesModal" data-patient-id="<?php echo $admission_patient_id; ?>"> Add Note </button>

2. 修改add_carenote.php中的模态框代码

给隐藏输入框添加固定ID,同时将标题中的硬编码ID改为动态显示:

<div class="modal fade" id="carenotesModal" tabindex="-1" role="dialog" aria-labelledby="carenotesModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">深夜Just�awip喜ack DIAG近数百万ures 继续 reserve / >
      <div class="modal-header">
        <h4 class="font-weight-bold text-primary" id="carenotesModalLabel">Add a care note - <span id="modal-patient-id"></span></h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="submit_carenote.php" method="post">
          <p><label for="new_note">Enter your note below:</label></p>
          <textarea id="new_note" name="new_note" rows="4" cols="50"></textarea>
          
          <p><br> Make this note public? 
            <select id="public" name="public">
              <option selected="selected">No</option>
              <option>Yes</option>
            </select>
          </p>
          
          <input type="submit" name="form1" value="Add Care Note" class="form_submit">
          <input type="hidden" name="patient_id" id="modal-patient-id-input" value="">
        </form>
      </div>
    </div>
  </div>
</div>

3. 添加JavaScript代码

监听模态框的show.bs.modal事件,从触发按钮中获取patient_id并赋值给模态框内的字段:

$('#carenotesModal').on('show.bs.modal', function (event) {
  const button = $(event.relatedTarget);
  const patientId = button.data('patient-id');
  const modal = $(this);
  
  modal.find('#modal-patient-id').text(patientId);
  modal.find('#modal-patient-id-input').val(patientId);
});

方案二:每行生成对应ID的模态框(纯PHP实现)

这种方案会为每一行记录生成一个唯一ID的模态框,无需依赖JavaScript。

1. 保留admissions.php中的按钮代码

确保按钮的data-target指向带当前patient_id的模态框ID:

// 循环内的按钮保持原写法:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#carenotesModal-<?php echo $admission_patient_id; ?>"> Add Note </button>

2. 在admissions.php循环内生成对应模态框

将模态框代码放到while循环内部,每个模态框使用唯一ID,并填充对应的patient_id:

// 在admissions.php的while循环末尾添加以下代码:
?>
<div class="modal fade" id="carenotesModal-<?php echo $admission_patient_id; ?>" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="font-weight-bold text-primary">Add a care note - <?php echo $admission_patient_id; ?></h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="submit_carenote.php" method="post">
          <p><label for="new_note-<?php echo $admission_patient_id; ?>">Enter your note below:</label></p>
          <textarea id="new_note-<?php echo $admission_patient_id; ?>" name="new_note" rows="4" cols="50"></textarea>
          
          <p><br> Make this note public? 
            <select id="public-<?php echo $admission_patient_id; ?>" name="public">
              <option selected="selected">No</option>
              <option>Yes</option>
            </select>
          </p>
          
          <input type="submit" name="form1" value="Add Care Note" class="form_submit">
          <input type="hidden" name="patient_id" value="<?php echo $admission_patient_id; ?>">
        </form>
      </div>
    </div>
  </div>
</div>
<?php
// 继续循环
}
?>

表单提交处理(submit_carenote.php)

不管用...哪种Cur Text['.github,`方案报类似在此特快�试图Mel的ec,都需要一个处理提交的页面,接收数据并插入数据库:

<?php
// 复用你的数据库连接
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['form1'])) {
  $patientId = $_POST['patient_id'];
  $newNote = $_POST['new_note'];
  $isPublic = $_POST['public'];
  
  // 准备插入语句
  $stmt = $conn->prepare("INSERT INTO rescue_carenotes (patient_id, note, is_public, created_at) VALUES (:patient_id, :note, :is_public, NOW())");
  $stmt->bindParam(':patient_id', $patientId, PDO::PARAM_INT);
  $stmt->bindParam(':note', $newNote, PDO::PARAM_STR);
  $stmt->bindParam(':is_public', $isPublic, PDO::PARAM_STR);
  
  if ($stmt->execute()) {
    header("Location: admissions.php");
    exit();
  } else {
    echo "Error adding care note.";
  }
}
?>

内容的提问来源于stack exchange,提问作者Dan Lindley

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:34:57