Datatables link pointing to incorrect link path

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

@poorbaldrick,

Is this a JavaScript issue or a PHP issue?

I think it is js issue. thanks

What value is stored in the database for this link?

EDIT: Derp, thats what you meant by mysql entry.

Your link isnt a link.
If you want it to behave like an absolute link, specify it in the database as
http://localhost/lsorg/admin/requests/destcerts/DEMO/DEMO_15-09-2018_169371_cert.pdf

Assume you’re connected to “http://127.0.0.1/john/index.html”
Telling a browser the link is “anything/something.html” tells the browser: “Whatever directory you’re currently in, there’s a subdirectory called anything, and something.html is inside it.” Your browser goes to http://127.0.0.1/john/anything/something.html
Telling the browser the link is “/anything/something.html” tells the browser: “Whatever server you’re currently connected to, cut off all the directories and go to anything/something.html.” Your browser goes to http://127.0.0.1/anything/something.html
Telling the browser the link is “http://localhost/anything/something.html” tells the browser “Ignore everything about where you are now, and just go here.” Your browser goes to http://localhost/anything/something.html"

1 Like

Thanks very much for detailed explanation and have used the absolute path in db as suggested. Many thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.