EDIT I just realized I had to json_encode
the fromDate that I am passing as argument to the URL in the AJAX call. So I need to use
url: 'json-responses.php?fct=exportTo...ByDate®Date=' + <?php echo json_encode($fromDate) ;?>
instead of
url: 'json-responses.php?fct=exportTo...ByDate®Date=' + <?php echo $fromDate ;?>
I took the wrong URL from a jqx.dataAdapter
where you can pass the date without json_encode
ing it first. Excel export still not working but I managed to get the requested data from the dB and output it on the page
EDIT-END
on an existing webpage I am trying to imlement a button which exports data to Excel. At first I tried to create a simple php file with the following code
source.php
<form method="post" action="export.php">
<input type="submit" name="export" value="Export" />
</form>
the export.php has the needed PDO .dsn data then it calls the exporting function exportToExcelMembersByDate($PDOdbObject, $regDate)
export.php
exportToExcelMembersByDate($pdo, "2013-09-12");
function exportToExcelMembersByDate($PDOdbObject, $regDate)
{
.
.
.
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
$output
contains a table in <html>
. When I click the button I immediately get the pop-up window asking if I want to save or open the excel file.
Now I wanted to implement the same solution in the existing environment.
I copy the function exportToExcelMembersByDate
to the existing data-layer.php
in the corresponding webpage I have the following
Source.php
<?php
$fromDate = $_POST['fromDate'];
?>
<script>
$("#export").click(function ()
{
exportToExcel();
}
);
function exportToExcel()
{
$.ajax({
type: 'GET',
dataType: 'html',
url: 'json-responses.php?fct=exportToExcelMembersByDate®Date= + <?php echo $fromDate ;?>,
})
}
</script>
<div>
<input type="button" id="export" value="Export to Excel" />
</div>
json-responses.php
if ($_GET['fct'] == 'exportToExcelMembersByDate')
{
$result = exportToExcelMembersByDate($connectionObject, $_GET['regDate']);
echo $result;
}
But here when I click on the button nothing happens. In AJAX I tried.
async: true/false
dataType: json/txt/html
type: 'POST'/'GET'