0

I'd like to make an ajax call to a php file; eventually this php file will write to a mongodb database. For now, I am debugging and would like to have the php file open and write to a text file. The php file works when I run it from the command line. Similarly, the ajax call returns a 'success' message. However, the ajax call does not result in the writing of a new text file.

From the relevant part of my javascript file:

$.ajax({
    type: "POST",
    url: "jq.php",
    data: {
        'myString': "blahblah",
    },
    success: function(data) {
        alert('worked');
    },
    error: function(){
        alert('failed');
    }
});

The php file:

<?php
$file=fopen("welcome.txt","w");
$str = "let's hope this works.";
fwrite($file, $str);
fclose($file);
?>

I have also tried wrapping the above content in: if ($_POST['ajax']) {...}.

4
  • 1
    remove the ajax, run the php file by itself with error reporting on Commented Jul 4, 2013 at 3:47
  • There's no error when I run the php file by itself; it opens and writes to the text file. Commented Jul 4, 2013 at 3:52
  • is jq.php in the same directory as the html file? run fire-bug(if using ff), check the "net" page Commented Jul 4, 2013 at 4:20
  • I tried running the code in a php file and made ajax call from the firefox console, it works fine for me, writes the file, even i could get the POST argument 'myString'. if your php or javascript code contains any other parts, the error may exist there. Commented Jul 4, 2013 at 4:30

2 Answers 2

1

For debugging, don't run the script from command-line but simply open it directly with your browser. When you're using the command-line, you're executing the script as the user you are (maybe "root"), but if it's called from your browser, it's executed by apache (or whatever webserver you're using) with it's own user.

Most likely, your own user account has writing permissions, but the apache-user doesn't - so the command-line works, but calls from a browser don't.

By the way: this is barely a good test case to replace a mongo-db (or any other database) interaction, where you won't have to worry about file-system-permission problems but could instead run into hundreds of other problems.

Sign up to request clarification or add additional context in comments.

Comments

0

you should always use:

$file=fopen("welcome.txt","w");
if ($file) { // because fopen can return FALSE on error
  $str = "let's hope this works.";
  fwrite($file, $str);
  fclose($file);
}
else {
  // something wrong!
}

ref

EDIT also check for E_WARNING - it can show you the problem, you can activate it by error_reporting(E_ALL); if it's disabled by your server or look for error inside php log file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.