I have a download page that needs to run a function when clicking on a link. The function is supposed to enter a row in a mysql database table and download the file. I don't know if I've coded the function incorrectly or I coded the onclick incorrectly. Here's the start of the page:
<?php
$filename = NULL;
session_start();
// start of script every time.
// setup a path for all of your canned php scripts
$php_scripts = '/home/larry/web/test/php/'; // a folder above the web accessible tree
// load the pdo connection module
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
require $php_scripts . 'mydloader.php';
//*******************************
// Begin the script here
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone")):
{
echo "Failed to connect to database" ;
exit;
}
endif;
//exit;
?>
<DOCTYPE html>
<html lang="en">
<head>
<title>Download</title>
<script type="text/javascript">
function myFunc($arg) {
var filename
filename = $arg
ip = GetUserIpAddr();
$stmt = $pdo->prepare("INSERT INTO download (IP_ADDRESS, FILENAME) VALUES (?, ?)");
$stmt->execute([ip,filename]) ;
header('Content-Type: octet-stream');
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
header('Pragma: no-cache');
header('Expires: 0');
readfile("download/{filename}");
}
</script>
Here's the section with the call to the function:
<!-- DOWNLOAD -->
<div id="download" class="stylized">
<div "myform">
<div class="container">
<div class="row">
<div class="download">
<br /><br>
<h1><center>FoxClone Download Page</center></h1>
<?php
$isos = glob('download/*.iso');
$iso = $isos[count($isos) -1];
$isoname = basename($iso);
$md5file = md5_file($iso);
?>
<div class="container">
<div class="divL">
<h3>Get the "<?php echo "{$isoname}";?>" file (approx. 600MB)</h3>
<a href="#" onclick="myfunc("<?php echo "{$isoname}";?>)";><img src="images/button_get-the-app.png" alt=""> </a>
Thanks for any help in advance,
Larry