Save values to file using php

error77
error77 used Ask the Experts™
on
Hi all,

How can I click on a button and save a string to a file in php please?

For example:MainPage ... contains a button or link ...

I click on it and it saves: eg: $var1 = 'somestring';

Then, if I click another button for $var1 .. it would replace 'something' with 'somethingElse'

Very Important: I don't want the page to reload everytime a press a button to save.

Can anyone help please?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Aaron TomoskyDirector of Solutions Consulting

Commented:
File_put_contents();
But that can't run at the press of a button. You would have to make another page that has this function, and use JavaScript to make an Ajax call to that page that saves the file.

Author

Commented:
hmm....

Tried this:

<a href="#" onclick="<?php echo file_put_contents("test.txt","Hello World. Testing!"); ?>">Save Me</a>

and it saves the data and creates the file even if I don't click on the link :o/

It would be perfect if I could control that.

Aaron TomoskyDirector of Solutions Consulting

Commented:
Right. Php is serverside. It will "do" that code when the page loads. If you want to do anything after page load, you have to do it in js. The js can call another file onclick though. So put the save code in a different file. Let's call it save.php. It can take get or post vars for filename and data.
Call save.php with js. That way you don't leave the page you are on.

Author

Commented:
I have no idea on how to code that i js :o/
Director of Solutions Consulting
Commented:
HeRe is a way too complicated example.
http://www.w3schools.com/php/php_ajax_poll.asp

Same basic thing tho. It uses js to call php, write a file (it uses fopen which I never use, just use fileputcontents. Way easier) and returns the result without refreshing the page.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial