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.

7
  • 3
    What's wrong with: <a href='/wp-content/plugins/est_collaboration/php/export_csv.php'>click</a> Commented Sep 6, 2016 at 13:02
  • @freedomn-m whats wrong with this??? i have not tried <a href=''></a> Commented Sep 6, 2016 at 13:04
  • @freedomn-m its perfectly working fine.But why its not coming to the above code.\ Commented Sep 6, 2016 at 13:11
  • @JMR JavaScript to accomplish what a simple plain link could do is just needlessly complex Commented Sep 6, 2016 at 13:11
  • What's the actual problem? That you can't download a file or that you don't get an event when you click a button? Commented Sep 6, 2016 at 13:13

2 Answers 2

2

You can simply try this

<a href="/wp-content/plugins/est_collaboration/php/export_csv.php">Download</a>
Sign up to request clarification or add additional context in comments.

1 Comment

i agree with your answer ..But what if i use button onclick function i need to know why its not coming.
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?

2 Comments

Glad to help, but interested in which bit fixed it for you (I can edit to remove the irrelevant part).
i used normal <a href></a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.