title: XMLHttpRequest() and outputting csv file
body: <p>I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to respond with:</p>
  
  <pre><code>header('Content-type: text/csv');
  header('Content-Disposition: attachment; filename="'.date("Ymdhis").'.csv"');
  print get_lines();
  </code></pre>
  
  <p>However, this leaves the the window open and blank.</p>
  
  <p>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.</p>
  
  <p>I have tried:</p>
  
  <pre><code>var xhReq = new XMLHttpRequest();
  var parameters = "";
  
  for ( i=0; i&lt;formObj.elements.length; i++ ) {
    parameters += formObj.elements[i].name + "=" + encodeURI( formObj.elements[i].value ) + "&amp;";
  }
  
  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);
  </code></pre>
  
  <p>but this doesn't work because the response is simply printed as text to screen.  </p>
  
  <p>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?</p>