2

I am using AJAX to pass a javascript variable to PHP. Everything was working well and dandy, and then I changed the security of how my php functions will execute. Here is my javscript code:

function writetofile(file, wellname, api){
   $.ajax({
    type: 'post',
    url: 'process.php',
    data: { 
        api: api,
        wellname: wellname,
        fileName: file,
        writetoFile: true
    },
    success: function(data){
        output(api + " : " + wellname + " has been added to: " + file);
    }
});

}

PHP:

// process.php
if ($_POST['writetoFile'])){ // i need to check here if writetofile is true, which it is
    $api = $_POST['api'];
    $wellname = $_POST['wellname'];
    $fileName = $_POST['fileName'];
    $break = "\n";
    $str = $api. " : ". $wellname . $break;
    $file = fopen($fileName,"a");
    fwrite($file, $str);
    fclose($file);
}

when I execute the javascript code, it doesn't write to the file, and yes, I have checked to see if the ajax request was accepted. It is, but i tried doing:

PHP

if ($_POST['writetoFile'] === true)){}

but that doesn't seem to be working either. Thank you for your help.

5
  • 1
    What happens when you var_dump($_POST)? You're also not doing any error checking. Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jan 8, 2015 at 16:32
  • let me add that and see what comes up Commented Jan 8, 2015 at 16:33
  • 3
    Wow i feel really stupid, I am so sorry for asking this. I have been staring at this damn piece of code for the last hour only to find that I had an extra ) in my code... Please don't downvote this. Thank you. The error set up did hel me a lot though. Commented Jan 8, 2015 at 16:35
  • Is this your entire code or is it just a part of it? If this is really the code you are using, what bothers me is this: if ($_POST['writetoFile'])){ -> check the number of ). You have two. Commented Jan 8, 2015 at 16:36
  • Glad you found the problem @macas. Commented Jan 8, 2015 at 16:36

1 Answer 1

1

Add error reporting to the top of your file(s) right after your opening <?php tag

error_reporting(E_ALL); 
ini_set('display_errors', 1);

You'll find that you have an extra closing parentheses in your if statement -

if ($_POST['writetoFile'] === true))
                             ------^
Sign up to request clarification or add additional context in comments.

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.