R9102
R9102

Reputation: 727

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.

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.

Upvotes: 0

Views: 82

Answers (2)

fdomn-m
fdomn-m

Reputation: 28611

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?

Upvotes: 1

Passionate Coder
Passionate Coder

Reputation: 7294

You can simply try this

<a href="/wp-content/plugins/est_collaboration/php/export_csv.php">Download</a>

Upvotes: 2

Related Questions