我想使用 iframe 打印依賴于 $stud_no 的信息。當我在表中顯示 iframe 時,它會生成正確的 $stud_no。但是當我單擊打印按鈕時,它只顯示第一個 $stud_no。就像按鈕沒有在admin_print-app-form-view.php中獲取 idadmin_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> </td> </tr> } }?> </tbody></table>admin_print-app-form-view.php<?php
session_start();
include("connection.php");
$stud_no = $_GET['id'];
?>這是打印預覽。紅圈中的數字應該是我點擊的$stud_no,但它總是給我第一個stud_no
1 回答

aluckdog
TA貢獻1847條經驗 獲得超7個贊
問題是,當您為每個 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>
在上面的代碼中,我通過添加前綴來創建唯一的 id,studframe然后添加 ,stud_no使其成為id="studframe1",id="studframe2"依此類推。
然后,當引用該特定 iframe 時,我們將根據該唯一 id 獲取 iframe。
- 1 回答
- 0 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消