1

I want to write data on a file on the server when the user is clicking on a btton. I am using javascript. I tried the method describes here: Saving a text file on server using JavaScript But it doesn't work.

Here my javascript function:

   {
      if($('radio_dispcor_without').checked)
      {
        var data = new FormData();
        data.append("data" , "the_text_you_want_to_save");
        var xhr = new XMLHttpRequest();
        xhr.open
        ( 
           'post', 
           '/home/laetitia/Project/WebSite/Website/ChemAlive_Interface/write.php', 
            true
        );
        xhr.send(data);
     ui.hideDialog('DispCor_DB');
     ui.showDialog('without_test');}
     if ($('radio_dispcor_first').checked) {
         ui.hideDialog('DispCor_DB');
         ui.showDialog('first_test');
     }
     if ($('radio_dispcor_second').checked) {
         ui.hideDialog('DispCor_DB');
         ui.showDialog('second_test');
     }
 });

And my php code:

<?php
if(!empty($_POST['data'])){
$data = $_POST['data'];
$file = fopen("text.txt", 'w');//creates new file
fwrite($file, $data);
fclose($file);
}
?>

When clicking on my web page using the debugger console, it tells me: "No element found" pointing to the last line of php file.

Thanks for your help.

Laetitia

Community
  • 1
  • 1
Laetis
  • 1,337
  • 3
  • 16
  • 28
  • What's that `/ChemAlive_Interface/test.php' });` line in the middle of your JavaScript? As shown, the JavaScript doesn't parse (because of that line). – T.J. Crowder Feb 16 '15 at 10:31
  • While I improved your post, I found serveral syntax errors in your javascript. – Reporter Feb 16 '15 at 10:34
  • Sorry a mistake in the deleting of commented line – Laetis Feb 16 '15 at 12:13
  • @reporter Which errors? I am newby to javascript, as well as html or php... – Laetis Feb 16 '15 at 12:15
  • You've confused the path of the file and the URL for the file. AJAX works by submitting data to a live *url*, not the server path of the file. – Dan Smith Feb 16 '15 at 12:20
  • @Bulk Do you mean replacing '/home/laetitia/Project/WebSite/Website/ChemAlive_Interface/write.php' by 'write.php' (php in same dir than my html file? The problem is not it's not finding the php I can have it appear in the debugger with the mention: l9 element not found. – Laetis Feb 16 '15 at 12:55

1 Answers1

0

For those interested, the problem was the permissions. www-data was not allowed to write on directory.

Laetis
  • 1,337
  • 3
  • 16
  • 28