0

I have a Js code in my phtml like below.

    var data = new FormData();
    data.append("data" , "the_text_you_want_to_save");
    var xhr = new XMLHttpRequest();
    xhr.open( 'post', 'http://mysite/project/en/index/index/index', true );
    xhr.send(data);

In my next phtml file.

<?php 
        if(!empty($_POST['data'])){
            echo "hello";
        $data = $_POST['data'];
        $fname = "hello" . ".txt";

        $file = fopen("/var/www/html/project" .$fname, 'w');//creates new file
        fwrite($file, $data);
        fclose($file);
        }
    ?>

But this is not working. I need to save a json file which contains parameters as dom objects.

  • But this is not working how isn't it working? are there any errors? if you do print_r($_POST) is everything there? also note that the filename in your PHP is going to be projecthello.txt - did you mean to add a separator after /project? – user7290573 Aug 20 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.