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"
value="Button1" />
<input type="submit" name="button2"
value="Button2" />
</form>
</head>
</html>
CodePudding user response:
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
}