JMR JMR - 2 years ago 80
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.

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download