4

I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to respond with:

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.date("Ymdhis").'.csv"');
print get_lines();

However, this leaves the the window open and blank.

I want to either close the window automatically (on recognition of the Content-Disposition) or I would prefer to ignore the window completely and simply onClick, call XMLHttpRequest() send the form variables to the PHP, generate the data file and then prompt the user to save or open.

I have tried:

var xhReq = new XMLHttpRequest();
var parameters = "";
for ( i=0; i<formObj.elements.length; i++ ) {
 parameters += formObj.elements[i].name + "=" + encodeURI( formObj.elements[i].value ) + "&";
}
xhReq.open("POST", outputLocation, false);
xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhReq.setRequestHeader("Content-length", parameters.length);
xhReq.setRequestHeader("Connection", "close");
xhReq.send(parameters);
document.write(xhReq.responseText);

but this doesn't work because the response is simply printed as text to screen.

What I really want, is once I have all the data, to issue a new header() call without opening a new window - is this possible using javascript?

asked Apr 30, 2010 at 3:49

1 Answer 1

3

You need to send the POST request in a new window in order to do this. I would suggest that you create a hidden form and set it's target attribute to _blank

You should set the Content-disposition HTTP header on the server-side to force a download.

You are calling document.write, which dumps a text string into the browser, this is likely not what you wish to do.

answered Apr 30, 2010 at 4:24
Sign up to request clarification or add additional context in comments.

4 Comments

+1 - just to reiterate - The use of XMLHttpRequest in this case is not indicated. A simple hidden or dynamically generated form submission to '_blank' is appropriate
it looks like you're using a form already - you probably don't need to make a new hidden form, just add target="_blank" to your current form and ditch the javascript/XHR. also, be sure to include the proper headers if you want the file to initiate a download. in PHP: header("Content-Disposition: attachment; filename=someFileName.csv");
I am already using a new window - I'm trying to avoid this as it is not really required... Perhaps the question should be slightly rephrased - How can I do that and have the window auto-close when it realises that it has the Content-Disposition: attachment; filename=file.csv;
I am returning: header('Content-type: text/csv'); header('Content-Disposition: attachment; filename=file.csv"'); print $contents; to xhReq.responseText; to the

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.