将整个变量传递给boostrap模态[重复]

问题描述 投票:0回答:1

我有一个主表页面,它加载查询结果,侧面有一个菜单(或者更确切地说是一组按钮)。我的问题是,当我从表中加载模态时,我只能看到其中一条记录中的“id”,并且它不会动态调用该行的 ID。

表:

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

此表填充在admissions.php 页面上,并且按钮行位于每行的一个单元格内。我想要做的就是加载带有创收病人 ID 的模态,以便我可以在表单中使用它。该表单位于 add_carenote.php 中,它包含在模态中

招生.php

//Loop from admissions table
$stmt = $conn->prepare("SELECT *FROM rescue_admissions
INNER JOIN rescue_patientsON rescue_admissions.patient_id = rescue_patients.patient_id
WHERE rescue_patients.centre_id = :centre_id AND rescue_admissions.disposition = 
'Held in captivity'
ORDER by 'admission_location' ASC");
$stmt->bindParam(':centre_id', $centre_id, PDO::PARAM_INT);

// initialise an array for the results                         
$applicants = array();                         
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {                             

$admission_id = $row["admission_id"];
$admission_patient_id = $row["patient_id"]; 
$admission_date = $row["admission_date"]; 
$admission_name = $row["name"]; 
$admission_presenting_complaint = $row["presenting_complaint"];
$admission_date = $row["admission_date"];
$adm_format_date = new DateTime($admission_date);
$adm_format_date = $adm_format_date->format('d-m-Y <\b\r> H:i');                               

print 
'<tr>
 <td>' . $adm_format_date . '</td>                             
 <td class="align-middle"><b>' . $admission_name . ' <BR></td>  
 <td class="align-middle">' . $admission_presenting_complaint . '</td>                                
 <td class="align-middle">                          

<!-- icon button group --> 
<div class="btn-group" > 
<a href="https://rescuecentre.org.uk/view-patient/?patient_id=' . $admission_patient_id . '"
  type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top"
  title="Manage Patient Record"><i class="fas fa-file" ></i></a>
<button type="button" class="btn btn-primary" data-toggle="modal" data
 target="#carenotesModal"></button>
<button type="button" class="btn btn-info" data-toggle="modal" data-placement="top"
title="Medication Given"><i class="fas fa-syringe" ></i></button> 
<button type="button" class="btn btn-primary" data-toggle="modal" data-
target="#carenotesModal-'. $admission_patient_id .'"> Add Note </button> 
</td>';?>

这是add_carenote.php

中的模态文本
<div class="modal fade" id="carenotesModal" tabindex="-1" role="dialog" 
aria-labelledby="carenotesModal" 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</h4> 
- <?php echo $admission_patient_id ?>
                              
<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="" 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></td>

<input type="submit" id="submit" name="form1" value="Add Care Note" class="form_submit">
<input type="hidden" name="patient_id" value= "/// HERE is where ill need the variable ">
</form>
</div>

我在这里看到了很多帖子试图通过传递变量来达到相同的最终结果。理想情况下,我希望行中的“患者 ID”填充模式,以便我可以确保表单转到正确的患者记录。我希望它作为弹出窗口,这样用户就不必离开招生页面。

我读过的一些解决方案要么完全堵塞了我的页面,因此页面无法工作,或者模式根本无法加载。使用了一些解决方案(我认为 ajax/jquery - 脚本),但我想知道表单/POST 是否会更好?

php bootstrap-4 bootstrap-modal
1个回答
0
投票

目前,当页面最初呈现时,您的 ID 已由 PHP 预加载到模式中,并保持固定。这不允许您在打开特定记录的模式时动态设置 ID。

相反,您可以在打开包含 ID 的模式的按钮上设置数据属性,以便在页面中预先准备好每条记录的 ID - 然后您可以使用 JavaScript 来检测模式何时显示,并使用它从触发模式的链接/按钮中获取 ID,并将其放入模式内的隐藏字段中。这样每次加载模态时都会选择并重写相关 ID。

while
中的
Admissions.php
循环的PHP代码可能应该类似于我在下面制作的示例。

首先,为了输出大块 HTML,通常更容易阅读,而不是使用巨大的

print
语句来突破 PHP,然后在其中注入包含短 PHP 块的 PHP 变量。

其次,您会看到我已将准入 ID 设置为注入到触发模式的“添加注释”按钮的 HTML 中的

data-id
属性中。

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {                             

  $admission_id = $row["admission_id"];
  $admission_patient_id = $row["patient_id"]; 
  $admission_date = $row["admission_date"]; 
  $admission_name = $row["name"]; 
  $admission_presenting_complaint = $row["presenting_complaint"];
  $admission_date = $row["admission_date"];
  $adm_format_date = new DateTime($admission_date);
  $adm_format_date = $adm_format_date->format('d-m-Y <\b\r> H:i');     

?> 
<tr>
 <td><?php echo $adm_format_date; ?></td>                             
 <td class="align-middle"><b><?php echo $admission_name; ?></b><BR></td>  
 <td class="align-middle"><?php echo $admission_presenting_complaint; ?></td>                                
 <td class="align-middle">                          

    <!-- icon button group --> 
    <div class="btn-group" > 
      <a href="https://rescuecentre.org.uk/view-patient/?patient_id=<?php echo $admission_patient_id; ?>"
  type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top"
  title="Manage Patient Record"><i class="fas fa-file" ></i></a>
      <button type="button" class="btn btn-info" data-toggle="modal" data-placement="top"
title="Medication Given"><i class="fas fa-syringe" ></i></button> 
      <button type="button" class="btn btn-primary" data-toggle="modal" data-
target="#carenotesModal" data-id="<?php echo $admission_patient_id; ?>"> Add Note </button> 
  </div>
</td>
<?php

然后 HTML 表单代码只需要调整:

<div class="modal fade" id="carenotesModal" tabindex="-1" role="dialog" aria-labelledby="carenotesModal" 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</h4>
        - <span class="admissionIDDisplay"></span>

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

            <input type="submit" id="submit" name="form1" value="Add Care Note" class="form_submit" />
            <input type="hidden" name="patient_id" />
          </p>
        </form>
      </div>
    </div>
  </div>
</div>

最后,这个 JS/jQuery 代码将处理模式的“显示”事件,该事件在模式打开时运行(根据 boostrap 文档)。然后,它获取触发显示模式的按钮的上下文,并可以从按钮的

data-id
属性获取 ID,并使用它来填充模式中的显示和隐藏字段。

<script>
$(function () {
  //triggered when modal is about to be shown
  $("#carenotesModal").on("show.bs.modal", function (e) {
    //get data-id attribute of the clicked element
    var admissionPatientId = $(e.relatedTarget).data("id");

    //populate the form
    $(e.currentTarget).find(".admissionIDDisplay").text(admissionPatientId);
    $(e.currentTarget).find('input[name="patient_id"]').val(admissionPatientId);
  });
});
</script>

这是一个工作示例(当然使用静态 HTML 表,因为 PHP 不在 Stackoverflow 代码片段中运行 - 但它基于如果 SQL 查询返回两行,您的 PHP 将输出的内容):

$(function () {
  //triggered when modal is about to be shown
  $("#carenotesModal").on("show.bs.modal", function (e) {
    //get data-id attribute of the clicked element
    var admissionPatientId = $(e.relatedTarget).data("id");

    //populate the form
    $(e.currentTarget).find(".admissionIDDisplay").text(admissionPatientId);
    $(e.currentTarget).find('input[name="patient_id"]').val(admissionPatientId);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<table>
  <tr>
    <td>22/09/2024 09:00</td>
    <td class="align-middle">
      <b>Patient A</b><br />
    </td>
    <td class="align-middle">Broken arm</td>
    <td class="align-middle">
      <!-- icon button group -->
      <div class="btn-group">
        <a href="https://rescuecentre.org.uk/view-patient/?patient_id=123" type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top" title="Manage Patient Record"><i class="fas fa-file"></i></a>
        <button type="button" class="btn btn-info" data-toggle="modal" data-placement="top" title="Medication Given"><i class="fas fa-syringe"></i></button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#carenotesModal" data-id="123">Add Note</button>
      </div>
    </td>
  </tr>
  <tr>
    <td>13/10/2024 15:05</td>
    <td class="align-middle">
      <b>Patient B</b><br />
    </td>
    <td class="align-middle">Chest pains</td>
    <td class="align-middle">
      <!-- icon button group -->
      <div class="btn-group">
        <a href="https://rescuecentre.org.uk/view-patient/?patient_id=456" type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top" title="Manage Patient Record"><i class="fas fa-file"></i></a>
        <button type="button" class="btn btn-info" data-toggle="modal" data-placement="top" title="Medication Given"><i class="fas fa-syringe"></i></button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#carenotesModal" data-id="456">Add Note</button>
      </div>
    </td>
  </tr>
</table>

<div class="modal fade" id="carenotesModal" tabindex="-1" role="dialog" aria-labelledby="carenotesModal"
  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</h4>
        - <span class="admissionIDDisplay"></span>

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

            <input type="submit" id="submit" name="form1" value="Add Care Note" class="form_submit" />
            <input type="hidden" name="patient_id" />
          </p>
        </form>
      </div>
    </div>
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.