0

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.

jQuery(document).ready(function($){
console.log("plugin script loaded3");
$('#csv').click(function()
{
 alert("csv");
 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.

R9102
  • 687
  • 1
  • 9
  • 32

2 Answers2

2

You can simply try this

<a href="/wp-content/plugins/est_collaboration/php/export_csv.php">Download</a>
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
  • i agree with your answer ..But what if i use button onclick function i need to know why its not coming. – R9102 Sep 06 '16 at 13:09
1

Change your jquery startup to simply:

$(function() {
    $('#csv').click(function() {
        alert("csv");
    });
});

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:

$("#downloadlink")
    .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?

Community
  • 1
  • 1
freedomn-m
  • 27,664
  • 8
  • 35
  • 57