我正在使用 顯示資料DataTable,每個單元格都有“編輯、洗掉、生成 PDF”。使用 Ajax 的添加、編輯和洗掉功能作業正常。單擊“生成 PDF”時,它會在帶有單擊單元格按鈕資料的新選項卡中打開。但是,當我單擊其他按鈕時,它始終顯示第一個單元格的資料,即使 URL 包含該單元格的 id
如何基于單擊按鈕將資料傳輸到 PDF?
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = [];
$filtered_rows = $statement->rowCount();
foreach ($result as $row) {
$sub_array = [];
$sub_array[] = $row["cccEmployee"];
$sub_array[] = $row["irNumber"];
$sub_array[] = $row["caseType"];
$sub_array[] = $row["caseLocation"];
$sub_array[] = $row["startDateTime"];
$sub_array[] = $row["endDateTime"];
// $sub_array[] = $row["caseDesc"];
// $sub_array[] = $row["actionsTaken"];
// $sub_array[] = $row["caseDetails"];
// $sub_array[] = $row["caseNotes"];
// $sub_array[] = $row["caseRecommendation"];
$sub_array[] = '<a href="javascript:void(0)" name="update" title="Edit" id="'.$row["id"] .'">
<i ></i>
</a>';
$sub_array[] = '<a href="javascript:void(0)" name="delete" title="Delete" id="'.$row["id"] .'">
<i ></i>
</a>';
$sub_array[] = '<a href="/test-pdf/'.$row['id'].'" target="_blank" id="'.$row["id"] .'">
<input type="submit" name="generate_pdf" id="generate_pdf" value="Generate PDF"/>
</a>'; // This is the code for the PDF.
$data[] = $sub_array;
}
$output = [
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_reports(),
"data" => $data,
];
echo json_encode($output);
生成-pdf-report.php:
include('./TCPDF/tcpdf.php');
$query = "SELECT * FROM cases_reports";
$statement = $connection->prepare($query);
$statement->execute();
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ) {
$reportId = $row['id'];
$cccEmployee = $row['cccEmployee'];
}
// Define the variables
ob_start();
// make TCPDF object
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
// set document information
$pdf->SetCreator('Mushref'); // $cccEmployee
$pdf->SetAuthor('Abdulrahman Mushref'); // $cccEmployee
$pdf->SetTitle("IR Number here"); // $caseType $irNumber
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords("Case, PDF, report, 'caseType'"); // $caseType
// add a page
$pdf->AddPage();
// Remove default header/footer
$pdf->setPrintFooter(false);
// set some text to print
$content = '';
$content .= '
<div>
<h4 align="center"> TEST '.$reportId.'</h4>
<h4 align="center"> Employee Name: '.$cccEmployee.'</h4>
';
$content .= '</div>';
$pdf->writeHTML($content);
//Close and output PDF document
ob_end_clean();
$pdf->Output('test.pdf', 'I');
我正在使用這個庫進行路由,檢查 /test-pdf 因為它是 TCPPDF 代碼的 URL。
// FOR TESTING
get('/test-pdf', '/backend/reports/generate-pdf-report.php');
get('/test-pdf/$report_id', '/backend/reports/generate-pdf-report.php');
uj5u.com熱心網友回復:
您沒有將變數傳遞給 PDF 生成器 PHP 檔案,而且在 PDF 生成檔案 $row['id'] 中未使用,這就是您獲得相同資料的原因。
少數地方的變化將使事情按預期進行。
將 $row['id'] 值傳遞給 php 檔案。
$sub_array[] = '
<a href="/test-pdf/rid/'.$row['id'].'" target="_blank" id="'.$row["id"] .'">
<input type="submit" name="generate_pdf" id="generate_pdf" value="Generate PDF"/>
</a>'; // This is the code for the PDF.
get('/test-pdf/rid/$rid', '/backend/reports/generate-pdf-report.php');
然后在 php 檔案中使用它。
$id = $_GET['rid'];
$query = "SELECT * FROM cases_reports WHERE `id`= ?";
$statement = $connection->prepare($query);
$statement->bind_param("s", $id);
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/388578.html
上一篇:DynamicBottomNavigationBarItems拋出錯誤“items.length>=2”:不正確
