1

I have a jQuery variable. It's a text string. I want to create a .txt file on the server, then write that string to the text file.

How do I trigger this without reloading the page? I know PHP will have to get involved somewhere...

My attempts to do this with $.ajax(); haven't worked.

1
  • 3
    What do those attempts look like? You'll probably get better answers by providing some code you've already tried. Commented Jul 9, 2011 at 15:02

1 Answer 1

6

You could pass this variable to a php script:

var data = 'foo bar';
$.post('/foo.php', { data: data }, function(result) {
    // success
    alert('the data was successfully sent to the server');
});

or if you prefer using $.ajax:

var data = 'foo bar';
$.ajax({
    url: '/foo.php',
    type: 'POST',
    data: { data: data },
    success: function(result) {
        alert('the data was successfully sent to the server');
    }
});

and then inside your foo.php script read the data from $_POST["data"] and save the contents to some file on the server.

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

1 Comment

Perfect. Thanks for clarifying the part about $_POST["data"]. That's where I was getting caught before.

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.