【问题标题】:How to print using iframe? ID does not get when print button is click如何使用 iframe 打印?单击打印按钮时未获取 ID
【发布时间】:2020-07-21 08:36:21
【问题描述】:

我想使用 iframe 打印取决于 $stud_no 的信息。当我在表中显示 iframe 时,它​​会生成正确的 $stud_no。但是当我单击打印按钮时,它只显示第一个 $stud_no。就像按钮没有在 admin_print-app-form-view.php

中获取 id

admin_print-app-form.php

<table id="dataTable2" class="text-center">
    <thead class="text-capitalize">
        <tr>
            <th>NO.</th>
            <th>LAST NAME</th>
            <th>FIRST NAME</th>
            <th>MIDDLE NAME</th>
            <th>SEX</th>
            <th>CONTACT NO.</th>
            <th>ENTRY</th>
            <th>ACTION</th>
        </tr>
    </thead>
    <tbody>
        <?php
            $sql = "SELECT * FROM stud_acc";
            $result = $con->query($sql);
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                $iframeId = 'studframe' . $row['stud_no'];
                $stud_no = $row['stud_no'];
                $lastname = $row['lastname'];
                $firstname = $row['firstname'];
                $middlename = $row['middlename'];
                $sex = $row['sex'];
                $contact = $row['contact'];
                $entry = $row['entry'];?>
        <tr>
            <td><?php echo $stud_no ?></td>
            <td><?php echo $lastname ?></td>
            <td><?php echo $firstname ?></td>
            <td><?php echo $middlename ?></td>
            <td><?php echo $sex ?></td>
            <td><?php echo $contact ?></td>
            <td><?php echo $entry ?></td>
            <td>
                <iframe src="admin_print-app-form-view.php?id=<?php echo "$stud_no"?>" name="frame" id="<?= $iframeId ?>" style="visibility:hidden;height:0px;width:0px"></iframe>
                <button type="button" class="btn btn-roundedtb btn-info" onclick="document.getElementById('<?= $iframeId ?>').print();"><i class="fa fa-print"></i><span class="icon-name"> Print</span></button>
            </td>
        </tr>
        } }?>
    </tbody>
</table>

admin_print-app-form-view.php

<?php
    session_start();
    include("connection.php");
    $stud_no = $_GET['id'];
?>

这是打印预览。红色圆圈中的数字应该是我点击的$stud_no,但它总是给我第一个stud_no

【问题讨论】:

  • 此代码将为所有生成的 iframe 赋予相同的名称和 ID。 ID 必须在文档中是唯一的。然后您可以使用该 ID 来定位您要打印的 iframe。现在,无论您单击哪个按钮,它似乎都在打印相同的 iframe。浏览器中没有任何魔法可以假设/猜测您实际尝试定位的 iframe 是什么。您必须明确定位特定的 iframe。
  • @MagnusEriksson 是的,你是对的。我不断得到相同的 iframe。但是当表中 iframe 的可见性没有被隐藏时,它会显示正确的 stud_no。为什么会这样?
  • 您正在创建多个 iframe,并且您正在传递正确的值。问题只是你的 JS frames['frame'] 总是引用同一个 iframe,这意味着你在每个按钮上打印同一个 iframe。

标签: javascript php iframe printing


【解决方案1】:

问题在于,当您为每个 iframe 提供相同的名称和 id 时,javascript 只会为您提供它找到的第一个匹配项,并为所有按钮返回相同的 iframe。

我们需要给他们唯一的 id(我们可以完全跳过名字)。

注意:此代码假定 stud_no 是唯一值,就像表的主键一样。

在您的while-loop 中,创建一个唯一的 id:

while($row = $result->fetch_assoc()) {
    // Create a unique id using the stud_no
    $iframeId = 'studframe' . $row['stud_no'];

现在给 iframe 那个 id:

<iframe ... id="<?= $iframeId ?>" ... ></iframe>

并确保按钮引用该 ID:

<button ... onclick="document.title=''; document.getElementById('<?= $iframeId ?>').print();">...</button>

在上面的代码中,我通过在它们前面加上 studframe 来创建唯一 ID,然后添加 stud_no 使其变为 id="studframe1"id="studframe2" 等等。

然后,当引用该特定 iframe 时,我们会根据该唯一 ID 获取 iframe。

【讨论】:

  • 当我点击按钮时现在什么都没有发生:'(我尝试在名称中使用您的想法而不是 id,但它也没有工作。
  • 您可以编辑您的问题以添加尝试吗?我需要确定您的实施。 (不要更改当前问题,只需添加新代码作为编辑)。
  • 您好,先生!现在可以了。这就是我所做的。 &lt;iframe src="admin_print-app-form-view.php?id=&lt;?php echo "$stud_no"?&gt;" name="&lt;?php echo $iframeId; ?&gt;" id="iframeid" style="visibility:hidden;height:0px;width:0px"&gt;&lt;/iframe&gt; &lt;button type="button" class="btn btn-roundedtb btn-info" onclick="document.title='';frames['&lt;?php echo $iframeId; ?&gt;'].print();"&gt;&lt;i class="fa fa-print"&gt;&lt;/i&gt;&lt;span class="icon-name"&gt; Print&lt;/span&gt;&lt;/button&gt; 谢谢你的想法。真的很感激:)
  • @bwi - 要么将 id 设置为也使用变量,要么完全删除 id 属性。如前所述,id 的 必须 在文档中是唯一的。多个元素具有相同的 id 是无效的。
  • 好的,先生。著名的。非常感谢您的帮助☺️
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多