I am displaying data using DataTable
, each cell has "Edit, Delete, Generate PDF". Functions of Add, Edit and Delete using Ajax are working fine. The "Generate PDF" when clicked, it opens in a new tab with data of the clicked cell's button. However, when I click on the other buttons it always show the data of the first cell, even though the URL contains that cell's id
How can I transfer data to PDF based on the button click?
$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);
generate-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');
I am using This library for routing, check for /test-pdf as it's the url to the TCPDF code.
// FOR TESTING
get('/test-pdf', '/backend/reports/generate-pdf-report.php');
get('/test-pdf/$report_id', '/backend/reports/generate-pdf-report.php');
CodePudding user response:
You are not passing the variable to the PDF generator PHP file and also, in PDF generation file $row['id'] is not used, that is the reason you are getting the same data.
Change in few places will make things work as expected.
Pass the $row['id'] value to the php file.
$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');
And then inside the php file use it.
$id = $_GET['rid'];
$query = "SELECT * FROM cases_reports WHERE `id`= ?";
$statement = $connection->prepare($query);
$statement->bind_param("s", $id);
...