This is a very quickly written example - it works for me as a test but you will need to look at it carefully to make suitable edits to suit your final needs and environment.
The ajax function is invoked when an a within the list is clicked. The code looks at the event and uses the event.target to intercept the innerHTML for the a - that is the filename sent as a parameter ( via ajax ) to the php script. The PHP script checks that the method is POST and that there is a filename in the POST array - if it is OK then will process that file and send back the csv data. This could be edited so that only the filename rather than the full filepath is sent so that users do not know the actual filepaths used on your system!
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['filename'] ) ){
$filename=filter_input( INPUT_POST, 'filename', FILTER_SANITIZE_STRING );
if( $filename && realpath( $filename ) ){
ob_clean();
$hndl=fopen( $filename, 'r' );
$html=array();
$html[]="<table id='toggle-vis'>";
while ( ( $line = fgetcsv( $hndl ) ) !== false ){
$html[]="<tr>";
foreach( $line as $cell )$html[]="<td>$cell</td>";
$html[]="</tr>";
}
$html[]="</table>";
fclose( $hndl );
header( 'Content-Type: text/html' );
exit( implode( PHP_EOL, $html ) );
}
}
?>
<!doctype html>
<html>
<head>
<title>csv manipulations</title>
<script>
/*
basic ajax function
*/
function ajax(m,u,p,c,o){
/*
Method,Url,Params,Callback,Options
*/
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if( xhr.readyState==4 && xhr.status==200 )c.call( this, xhr.response, o, xhr.getAllResponseHeaders() );
};
var params=[];
for( var n in p )params.push( n+'='+p[ n ] );
switch( m.toLowerCase() ){
case 'post': p=params.join('&'); break;
case 'get': u+='?'+params.join('&'); p=null; break;
}
xhr.open( m.toUpperCase(), u, true );
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send( p );
}
/*
event handler assigned to each `a` within the list ( li.active > a )
*/
function getcsvdata(e){
try{
e.preventDefault();
var method='post';
var url=location.href;
var params={
filename:e.target.innerHTML
};
var callback=csvcallback;
var options={};
ajax.call( this, method, url, params, callback, options );
}catch( err ){
console.warn( err );
}
}
/*
ajax callback to process the response from the php script at the top
*/
function csvcallback( data ){
try{
var table=document.getElementById('toggle-vis');
if( table ){
var parent=table.parentNode;
parent.removeChild( table );
parent.insertAdjacentHTML('beforeend',data);
}
}catch( err ){
console.warn( err );
}
}
/*
Bind event handlers to each `a` in list
*/
document.addEventListener('DOMContentLoaded',function(){
var col=Array.prototype.slice.call( document.querySelectorAll('li.active > a') );
col.forEach(function(e){
e.addEventListener('click',getcsvdata,false);
});
}, false );
</script>
</head>
<body>
<h1>CSV Files</h1>
<ul id='list'>
<?php
/* edit to suit */
$dir='c:/temp2/csvfiles';
$col=glob( $dir . '/*.csv' );
if( !empty( $col ) ){
foreach( $col as $file ){
echo "<li class='active'><a href='#'>$file</a></li>";
}
}
?>
</ul>
<table id='toggle-vis'>
<tr>
<td>csv data to appear here when links clicked</td>
</tr>
</table>
</body>
</html>