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 8113015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:41:28+00:00 2026-06-06T02:41:28+00:00

This javascript function takes JSON and formats it to XML. The data is a

  • 0

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.

 json2xml(eval(data));

JS

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

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.

 $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;
 }

}

This is currently returning a server 500 error.

  • 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-06T02:41:29+00:00Added an answer on June 6, 2026 at 2:41 am

    Updated:

    With your provided jQuery AJAX call:

    $.ajax({
       type: 'POST',
       url: 'download/yourphpscript.php',
       data: { xml: xml2 },
       dataType: XML
    });
    

    Your PHP would look like this:

    <?php
    $xml = $_POST['xml'];
    // Not sure where you're trying to get $file from
    
    writetoxml($xml, $file);
    
    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 download($file)
    {
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }
    ?>
    

    Try passing an object (in this case { key: value } into the data property of the $.ajax call so that you can reference it by your key. In this case our key is xml so on the PHP side, we grab $_POST['xml'] and that should give you the contents of xml2.

    The download code was taken from the PHP docs on readfile(). However, I still can’t help but think there is a better way to accomplish this.

    I would HIGHLY recommend against allowing the user to effectively create a web accessible file containing whatever they want on your server. As aforementioned, a clarification of your general goal would be helpful. I think I understand what you’re trying to do but why you are doing it would be nice to know as there may be a better and safer way to accomplish the same result.

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

Sidebar

Related Questions

Objective: I would like to take a JSON String and pass it into JavaScript
Assuming I have a JavaScript function like this... function Object1() { this.var1; this.var2; this.var3;
I have a Javascript function that takes quite a long time to run. I
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
I am using this JavaScript function in order to change image on click, but
So I have this javascript function, it sends an ajax requests to fetch a
I am having problem with this javascript function. If I want to submit a
I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var
Given this HTML: <form id=formx> <input type=text value= /> </form> And this JavaScript: $(function()
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();

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.