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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:14:46+00:00 2026-06-10T22:14:46+00:00

I just want to save some JSON (generated with Javascript) in a file on

  • 0

I just want to save some JSON (generated with Javascript) in a file on the server.
But I even don’t get it to work with a simple string:

HTML File:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<style>
#test{
padding:20px 50px;
background:#ccc;
color:#000;
}
</style>
<script>
$(function(){
$('#test').click(function(){
    $.ajax({
        url: "page.php",
        data: {"foo": "bar"},
        processData: false,
        contentType: 'application/json'
    });

});
});
</script>
</head>
<body>

<div id="test">
KLICK
</div>
</body>
</html>

And the php file is something like this:

<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
fwrite($fh,$_POST['data']);
fwrite($fh,$_POST['foo']);
fwrite($fh,$_POST["foo"]);
fwrite($fh,$_POST[foo]);
fclose($fh);

Nothing worked. I also tried

$.ajax({
    var datatosend="foo bar";
    url: "page.php",
    data: datatosend,
    processData: false
    });

I don’t know what could be wrong. The txt file is there after clicking the div in the html file. But there is no content in the file. If I just write $_POST to the text file, the file contains the Text “Array”, meaning that $_POST has some content.

  • 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-10T22:14:48+00:00Added an answer on June 10, 2026 at 10:14 pm

    Few things could be going wrong here. Check the permissions on the directory your are trying to write to. Also make sure that your ajax call is using the POST method.

    $.ajax({
      url: "page.php",
      type : 'POST',
      ...
    }); 
    

    As sated in the jQuery documentation, the type parameter sets the request type, POST or GET.

    The type of request to make (“POST” or “GET”), default is “GET”.


    Another thing to consider is that you are not actually writing JSON data. The data is send in that format but when it gets to the $_POST variable it has been converted into an array. What you should try doing is writing a PHP array to the file –

    $fh = fopen($myFile, 'w');
    fwrite($fh,'<?php $arr='.var_export($_POST['data'],true).' ?>');
    fclose($fh);
    

    That should give a file similar to this –

    <?php
      $arr = array(
        'foo'=>'bar'
      )
    ?>
    

    As you can see, the var_export() function returns a parsable version of the variable.

    • var_export()

    var_export — Outputs or returns a parsable string representation of a variable

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

Sidebar

Related Questions

i just want to ask, how can i save my json file from url
I want to save some values before i closed the app. But i don't
Basiclly I just want to capture audio and save it to a file on
Just want to ask can netbean IDE get connected to clearcase remote client server
I simply just want to count the clicks of a button and save the
What is the best way to save a file from internet on javascript and/or
I want my 2D sprites move just along some given paths(provided both position and
I create a huge JSON-Object and save it in my database. But when I
I just want to browse my Subversion code repositories and view files. Some of
This question seems simple at first glance, but has slowed down my work! Please,

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.