0

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&regDate=' + <?php echo json_encode($fromDate) ;?>

instead of

url: 'json-responses.php?fct=exportTo...ByDate&regDate=' + <?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;
    }

$outputcontains 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 exportToExcelMembersByDateto 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&regDate= + <?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'

Alle V.
  • 19
  • 1
  • 9
  • 1
    Possible duplicate of [download file using an ajax request](https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request) – Anthony May 24 '18 at 20:06
  • There is no reason to make the request via Ajax since making it through the form downloads the file without navigating away from the current page. – Anthony May 24 '18 at 20:08
  • @Anthony: I tried `windows.location...` however this did not work with the existing file structure. How do I launch a php function in the data-layer.php file from a JavaScript in another file? Or is the only solution to create a separate `export.php` file for every table I want to export and to call it like I did in my test case? – Alle V. May 24 '18 at 20:58
  • You mean how do you pass the from date to export.php? – Anthony May 24 '18 at 20:59
  • no, the question is rather can I launch the export function in the data-layer.php file instead of using the export.php file – Alle V. May 24 '18 at 21:16
  • You are currently trying to fetch the file via a url `json-responses.php`. Why not just have your button submit a form to that URL like before? Or use `window.location = json-responses.php`? I don't really understand the significance of this data layer you're bringing up, but if you just want to avoid response.php and use a different script/url, it doesn't seem like that limits you to an ajax request. – Anthony May 24 '18 at 21:20
  • Also, this part doesn't make sense : `$fromDate = $_POST['fromDate'];`. Does that mean you are having the user submit a form and then outputting ajax to then make a second request for the file? Why not just make the button a link with `href="json-responses.php?fct=exportToExcelMembersByDate&regDate=2013-09-12 ` ? (That url is also pretty sloppy, FYI. Don't you need to have a separator of some kind for the fromDate?) – Anthony May 24 '18 at 21:26
  • @Anthony, yes you are right! a simple button with the link to `json-responses.php?fct=exportToExcelMembersByDate&regDate=` is doing the trick It is working now. no need for Ajax Request. – Alle V. May 25 '18 at 21:39

0 Answers0