I have a JS function that is embedded inside of PHP. I declare the function first and then call it. I keep getting the error downloadCSV is not a function with the below code:
<?php
include_once "orders.php";
$body = "";
$body .= "<form method = 'post' action='" . $_SERVER['PHP_SELF'] . "'><input type='text' placeholder='begin date' name='begin_date'>
<input type='text' name='end_date' placeholder='end date'><input type='submit' name='date_range'></form>";
if(isset($_POST['date_range']))
{
$orders = new Orders();
$body .= $orders->displayOrders($_POST['begin_date'], $_POST['end_date']);
$head = '<script>
//document.getElementById("downloadCSV").addEventListener("click", downloadCSV);
function downloadCSV(x,y)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log( this.responseText );
}
};
xhttp.open("POST", "downloadcsv.php", true);
xhttp.send();
}
</script>';
$body .= "<form name='createCSV' id='createCSV'><input type='button' id='downloadCSV' value='download csv' onclick='downloadCSV(\"" . $_POST['begin_date'] . "\", \"" . $_POST['end_date'] . "\");' /></form>";
}
?>
<html>
<head>
<?php
echo $head;
?>
</head>
<body>
<?php
echo $body;
?>
</body>
</html>
I cannot figure out why I keep getting the error when I clearly declare the function before calling it