-4

I'm trying to save a txt file from javascript by letting ajax call a php script. The callback is successful, however the php is not being executed. Also, running the php script in terminal will create my txt file. So somehow the call is not getting executed? What am I doing wrong?

The javascript:

var data = "test"
$.ajax({
    url: 'http://localhost/saver.php',
    type: 'POST',
    data: { data: data },
    success: function(result) {
        alert(result);},
    error: function(result) {
        alert('ERROR');}
        });

The php script:

<?php

$data = $_POST['data'];
$fp = fopen('path/hs2.txt', 'w');
fwrite($fp, $data);
fclose($fp);

?>
Ansjovis86
  • 1,506
  • 5
  • 17
  • 48
  • can you add `console.log(result)` just to check? – FirstOne Jul 30 '16 at 22:11
  • Can you show me the file structure? and where these both files (file which has ajax and the end php file) are? – Akshay Jul 30 '16 at 22:13
  • 1
    @FirstOne: it shows the php script – Ansjovis86 Jul 30 '16 at 22:15
  • @Akshay: they are in the same folder actually – Ansjovis86 Jul 30 '16 at 22:15
  • What do you mean the php script? the source code? – FirstOne Jul 30 '16 at 22:16
  • @j08691 maybe permissions aren't ok. What should I add? Shebang? And some kind of chmod? – Ansjovis86 Jul 30 '16 at 22:18
  • @Ansjovis86 Set permissions as `777` to *check it's working*. – Akshay Jul 30 '16 at 22:18
  • You can chmod it to 0777 temporarily – j08691 Jul 30 '16 at 22:19
  • 1
    @FirstOne it prints exactly the same thing as the contents of the php script – Ansjovis86 Jul 30 '16 at 22:19
  • 1
    Check these questions: [PHP code is not being executed, instead code shows on the page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) **&&** [Apache shows php code instead of executing](http://stackoverflow.com/questions/12142172/apache-shows-php-code-instead-of-executing) **&&** [**ajax: responsetext returns my entire php code locally**](http://stackoverflow.com/questions/10186009/ajax-responsetext-returns-my-entire-php-code-locally) – FirstOne Jul 30 '16 at 22:22
  • @j08691: permissions appear not to be the problem – Ansjovis86 Jul 30 '16 at 22:24
  • Just to test, replace what you have now (but not the POST array) with an echo for it, see if something gets echo'd. – Funk Forty Niner Jul 30 '16 at 22:25
  • @FirstOne it might help if I read the comments ;) – j08691 Jul 30 '16 at 22:27
  • "It shows the php script" -- Is your WAMP server even running? – Akshay Jul 30 '16 at 22:27
  • If it shows the PHP script, then if you're accessing this as `file:///file.xxx` in your browser, then that's the problem. Is PHP installed? running off a host? doesn't seem to be parsing to me. If local, use `http://localhost/file.xxx` – Funk Forty Niner Jul 30 '16 at 22:28
  • I changed all file and folder permissions. This is definitely not the problem. This script is being run locally and deals with local files. – Ansjovis86 Jul 30 '16 at 22:32
  • [This is the comment that needs addressing...](http://stackoverflow.com/questions/38678918/php-not-executing-after-ajax-call#comment64737636_38678918) as to "how" it's accessed (2 different animals). The question's short on detail here. – Funk Forty Niner Jul 30 '16 at 22:34
  • @Fred-ii- PHP is installed (I can run it in terminal). The html file in which the ajax part is included is running locally on my computer. Trying the localhost url is resulting now in calback errors on ajax. – Ansjovis86 Jul 30 '16 at 22:42
  • I've put in the full javascript now. So you can try at home ;) – Ansjovis86 Jul 30 '16 at 22:45
  • Alright, to clear things out: replace `$_POST['data']` with `'some string'` and access the php script **from the browser**. You should access something like `localhost/saver.php` (put the correct path to the php file, just make sure the url does **not** start with `file:///`) – FirstOne Jul 30 '16 at 22:47
  • 1
    Ok, the html that I run has indeed a path with file:///path/myscript.html . I cannot seem to switch properly to localhost. It is saying OSX is not recognizing adresses starting with "localhost". – Ansjovis86 Jul 30 '16 at 22:57
  • @FirstOne So what was this story about WAMP or MAMP? I might need that, no? – Ansjovis86 Jul 30 '16 at 23:07
  • 1
    Yeah that did the trick. Thanks guys. – Ansjovis86 Jul 30 '16 at 23:29

1 Answers1

1

It's a path issue in fopen(...). Change it to:

$fp = fopen('hs2.txt', 'w');

fopen() cannot create a file in a folder that doesn't exist. You are trying to create a file in a non-existent nested folder.

Marios Fakiolas
  • 1,525
  • 1
  • 12
  • 19