Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:08:57+00:00 2026-06-13T09:08:57+00:00

I am trying to build a CSV file in PHP, then call the PHP

  • 0

I am trying to build a CSV file in PHP, then call the PHP file from an AJAX call, which will then initiate a download of the CSV file upon success of the AJAX call. This works fine if I save a physical copy of the .csv on the server, but I would like to use php://ouput so I do not have to worry about physical files clogging up the server. Is it possible to initiate a download from returning php://output to AJAX? Here is my code:

HTML/jquery:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript" language="javascript" src="jquery.js"></script>
        <script type="text/javascript">
             $("#download").live("click", function() {
                var request = $.ajax({
                    dataType: 'html',
                    url: 'php.php',
                    success: function(response) {
                        alert('Finished');
                    }
                })
            })
        </script>
    </head>
    <body>
        <h1 id="download">DOWNLOAD</h1>
    </body>
</html>

PHP:

<?php 
    header('Content-type: application/vnd.ms-excel');
    header('Content-disposition: attachment; filename="test.csv"');
    $f = fopen('php://output', 'w');
    fwrite($f,'this,is,a,test');
    fclose($f);
    readfile('php://output');
    return;
?>

I am not sure how to get this to return a File Save dialog from my AJAX call.

This has to be simple, but I can’t seem to find any examples that combines these two issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:08:58+00:00Added an answer on June 13, 2026 at 9:08 am

    The following works, but is highly inneficient as it calls the php.php file twice. Does anybody have any better ideas?

    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <script type="text/javascript" language="javascript" src="jquery.js"></script>
            <script type="text/javascript">
                 $("#download").live("click", function() {
                    var request = $.ajax({
                        dataType: 'html',
                        url: 'php.php',
                        success: function(response) {
                            window.open('php.php');
                        }
                    })
                })
            </script>
        </head>
        <body>
            <h1 id="download">DOWNLOAD</h1>
        </body>
    </html>
    

    Is there anyway to cache ‘php.php’ for just this instance so that it loads instantly under window.open('php.php'), but will reload contents when I click download next?

    Why does window.open(response) not work the same?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
I am trying to build a import form for a CSV file into the
I'm trying to load data from an external csv file that needs to be
I am trying to read from a csv file and insert the data into
I am trying to output data from an array to a csv and download
I'm trying to open a CSV file, look up a string, and then return
I am currently trying to process a csv file in PHP using preg_match(). An
I am trying to import a CSV file into my PHP application built with
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.