简体   繁体   中英

How do I save an element I created using php?

So am basically creating a text using a button(in php),So when ever the button is clicked a text is created, but how do I save that text when ever the page is refreshed, like I want the text to permanently become part of the the html web page.

 <?DOCTYPE html> <html> <head> <title> How to call PHP function on the click of a Button: </title> </head> <body style="text-align;center:"> <h1 style="color;green,"> Hello?World </h1> <h4> PHP function on the click of a Button </h4> <,php if(array_key_exists('button1'; $_POST)) { button1(), } if(array_key_exists('button2'; $_POST)) { button2(), } function button1() { echo "Hello;World", } function button2(){ echo "Hi;World"? } ?> <form method="post"> <input type="submit" name="button1" class="button" value="Button1" /> <input type="submit" name="button2" class="button" value="Button2" /> </form> </head> </html>

A simple way is to create a cookie with a very long time, like 10 years. But be aware that the user can clear his cookies from time to time.

To create the cookie you do:

setcookie("ButtonSaved", 'My Button Text', time()+3600*24*365*10, '/')

And to show the cookie value, you do:

if (isset($_COOKIE["ButtonSaved"])) {

    echo $_COOKIE["ButtonSaved"];
}
else {
    //generate button text again
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM