Hi
I am using datatables to display data which works fine. The problem I am having is that I am using a link in a column and for some reason, the link when downloaded shows incorrect link path.
For example, in mysql the link path is correct
localhost/lsorg/admin/requests/destcerts/DEMO/DEMO_15-09-2018_169371_cert.pdf
but when I click on link, it errors with:
The requested URL /lsorg/admin/localhost/lsorg/admin/requests/destcerts/DEMO/DEMO_15-09-2018_169371_cert.pdf was not found on this server.
It seems to be placing extra data before the link value. I would be grateful if someone check my code for the error as I have checked for hours and cannot see where the error is. Many thanks.
certificateData.php
<?php session_start(); ?>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/lsorg/admin/Connections/domain.php'); ?>
<?php
$sql = "SELECT * FROM certificates ORDER BY date ASC";
$result = mysqli_query($conn, $sql) or die("SQL Error 1: " . mysqli_error($conn));
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$date = date('d/m/Y', strtotime($row['date']));
$link = $row['link'];
$data[] = array(
'id' => $row['id'],
'company' => $row['company'],
'certnumber' => $row['certnumber'],
'date' => $date,
'item' => $row['item'],
'link' => $link
);
}
echo json_encode($data);
} else {
echo '{
"sEcho": 1,
"iTotalRecords": "0",
"iTotalDisplayRecords": "0",
"aaData": []
}';
}
?>
datatables code
$(function() {
var link = '<i class="fa fa-file-pdf" style="align: middle; font-size: 2.1em; color: red; margin-right: 14px;"></i>' + ' Download Certificate';
tableCerts = $('#certfiles').DataTable({
"ajax": "/lsorg/admin/certificateData.php",
"bPaginate": true,
"bProcessing": true,
// "bserverSide": true,
"pageLength": 10,
"autoWidth": false,
"ordering": true,
"sortable": true,
"sAjaxDataProp": "",
"columns": [{
data: 'id'
}, {
data: 'company'
}, {
data: 'certnumber'
}, {
data: 'item'
}, {
data: 'date'
}, {
data: 'link',
"render": function(data, type, row, meta) {
if (type === 'display') {
data = '<a href="' + data + '" style="text-decoration: none;">' + link + '</a>';
//console.log(data);
}
return data;
}
}, ],
"order": [
[
4, "desc"
]
],
"columnDefs": [{
"width": "10%",
"targets": [1],
}, {
"width": "12%",
"targets": [2],
}, {
"width": "35%",
"targets": [3],
}, {
"width": "16%",
"targets": [4],
}, {
"orderable": false,
"targets": [0, 5]
}, {
className: "hide_id",
"targets": [0]
}],
});
});
mysql entry
localhost/lsorg/admin/requests/destcerts/DEMO/DEMO_15-09-2018_169371_cert.pdf