1

I've never really used AJAX before so I'm trying to get familiar. I have an html page:

<html>
<script type="text/javascript" src="jquery-1.12.3.min.js"></script>

<script>
    function write(){
        $.ajax({
        type: 'POST',
        url: "write.php",
        data: "something",
        success: function(result) {
            alert('the data was successfully sent to the server');
        }
        })
    }
</script>

Click to write some stuff
<a href="javascript:write()" class="write">Write</a>
</html>

And the associated write.php file in the same directory:

<?php
error_reporting(E_ALL);
$data = $_POST['data'];
$f = fopen('file.txt', 'w+');
fwrite($f, $data);
fclose($f);
?>

When I click the link, I get the success message, but the file is not created on the server. Not really sure what to investigate. Have confirmed I have write access to the directory

7
  • php.net/manual/en/function.error-reporting.php and look at your console Commented May 11, 2016 at 15:58
  • added the error_reporting line to php. Don't see any errors in the console when hitting the link Commented May 11, 2016 at 16:01
  • add ini_set('display_errors', 1); after that Commented May 11, 2016 at 16:02
  • 1
    $_POST doesn't contain what you think it contains. Commented May 11, 2016 at 16:02
  • ^ nope, my sentiments exactly. Commented May 11, 2016 at 16:02

1 Answer 1

3

Look, if you're going to use jQuery use it all of the way. First, your link shouldn't have any inline JavaScript.

<a href="#" class="write">Write</a>

When clicked you capture the click of your link event in your main jQuery code by its class (and stop the default behavior of the click):

<script>
    $(document).on('click', '.write', function(event) {
        event.preventDefault();

You're not sending what you think your are sending to the PHP script as the $_POST array expects key / value pairs. Create a key/value pair(s) for your data:

        $.ajax({
        type: 'POST',
        url: "write.php",
        data: {something: 'foo'}, // key value pair created, 'something' is the key, 'foo' is the value

Don't use alert() for troubleshooting., use console.log() instead.

            success: function(result) {
                console.log('the data was successfully sent to the server');
            }
        });
    });
</script>

Now, when you send the data you'll have something in $_POST['something']:

<?php
    error_reporting(E_ALL);
    $data = $_POST['something']; // the key we sent was "something"
    $f = fopen('file.txt', 'w+');
    fwrite($f, $data);
    fclose($f);
?>

Now the text file should contain "foo" because you have actually sent something to the PHP script which can be parsed. The only other thing you need to do is to make sure the PHP script has permissions to open and write to a file on your server.

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.