Avatar of error77
 asked on

Save values to file using php

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?


Avatar of undefined
Last Comment
Aaron Tomosky

8/22/2022 - Mon
Aaron Tomosky

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.


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 Tomosky

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

I have no idea on how to code that i js :o/
Aaron Tomosky

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question