JMR JMR - 6 months ago 27
jQuery Question

Is this right way to call php file using jquery?

I am trying to call the php file onclick of button ,
i tried using ajax but many of them said not to use for downloading the file.

here is my code.

console.log("plugin script loaded3");
document.location.href = "/wp-content/plugins/est_collaboration/php/export_csv.php";


Even tried using $get but its not getting downloaded.

when i click the button it has to call php file which download csv format.


Change your jquery startup to simply:

$(function() {
    $('#csv').click(function() {

Alternatively, to download a "file", you can add it as the href to an anchor link, eg:

<a id='downloadlink' href='/wp-content/plugins/est_collaboration/php/export_csv.p‌​hp'>csv</a>

if you need to add parameters, you can change the href via jquery, eg:

    .prop("href", "/wp-content/plugins/est_collaboration/php/export_csv.p‌​hp?param1=" + param1value);

Alternatively (again), it could be that you are adding the "#csv" button dynamically, after jquery startup has run, in which case you need event delegation. See this question for more info: Event binding on dynamically created elements?