Very stuck. I have some JS that passes a start and end date from a form to a php file that then selects the entries of that date range in the db and exports an excel file. The ‘date’ is correct, and as far as I can tell ‘data’ is set correctly in ajax. But when I check the apache_error.log, I’m getting:
PHP Warning: Undefined array key "startDate"
PHP Warning: Undefined array key "endDate"
$(document).on('submit', '#export_form', function(event){
event.preventDefault();
//this returns correct date 2024-05-01
console.log($("#startDate").val());
$.ajax({
url: 'exporttoexcel.php',
type: 'POST',
data: { startDate: $('#startDate').val(), endDate: $('#endDate').val() },
processData: false,
xhrFields: {
responseType: 'blob' // to avoid binary data being mangled on charset conversion
},
success: function(blob, status, xhr) {
// check for a filename
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=n]*=((['"]).*?2|[^;n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location.href = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location.href = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
$('#export_form')[0].reset();
$('#exportModal').modal('hide');
}
}
})
//window.open('exporttoexcel.php');
//document.getElementById('export_form').submit();
if(startDate != '' )
{
//window.open('exporttoexcel.php', '_blank');
}
else
{
alert("Fields are Required");
}
});
<?php
include('db.php');
include('function.php');
include('PhpXlsxGenerator.php');
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$start_date = $_POST['startDate'];
$end_date = $_POST['endDate'];
$sql = "SELECT r.id,
r.created_at AS 'Date Time', ...irrelevant
?>
Why is the post data not being passed along?