Sending JS variable data to a downloadable page
<p>This javascript function takes JSON and formats it to XML.  The data is a long XML string, whic I would like to allow the user to download. I'm trying to use ajax to post the data to a php page wichi will create the file and then allow the user to download it. </p>

<pre><code> json2xml(eval(data));
</code></pre>

<p>JS</p>

<pre><code> $.ajax({
   type: 'POST',
   url: 'download/functions.php',
   data: xml2,
   dataType: XML
 });
</code></pre>

<p>I have used this PHP function to write to a file, but I'm not sure how to now send the js variable to this function.</p>

<pre><code> $data = $_POST['xml2'];

 writetoxml($data, 'WF-XML'.$current. '.xml'); 

 function writetoxml($stringData, $myFile) {
    $current = date('m-d-Y-g-i-s');
    $fh = fopen('download/'.$myFile, 'w') or die("can't open file");
    fwrite($fh, $stringData);
    fclose($fh);
    download($file);
  }

 function downloadFile($file) {

 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {
     // Set headers
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/csv");
     header("Content-Transfer-Encoding: binary");

     // Read the file from disk
     readfile($file);
     exit;
 }
</code></pre>

<p>}</p>

<p>This is currently returning a server 500 error.</p>
