I am new to laravel and php and I am trying to change text after clicking button and keep it that way even after reopening window. I use sessions for this purpose.
<?php
$bool = $_SESSION["add"];
if ($bool == true) {
$text = "Remove from shopping cart";
?>
<button onclick="doSomething()" class="corner" id="test">{{$text}}</button>
<?php
} else {
$text = "Add to shopping cart";
?>
<button onclick="doSomething()" class="corner" id="test">{{$text}}</button>
<?php
}
function doSomething() {
if ($text === "Add to shopping cart") {
$_SESSION["add"] = true;
echo "<meta http-equiv='refresh' content='0'>";
} else {
$_SESSION["add"] = false;
echo "<meta http-equiv='refresh' content='0'>";
}
}
This is my code in the beginning I defined variable bool which is value from add in sessions. I bool is true or 1, it will set button with text Remove from shopping cart, else it will set text of button as Add to shopping cart. Tell me if I am wrong but I think this should run even if key add is not defined.utton function is doSomething, I am sorry for not very original name I will fix it afterwards. And the ideabehind this function is that if the text is Add to shopping cart, then it will set session key add as true and with function which i found here: Refresh page after form submitting and here:https://www.w3schools.com/tags/att_meta_http_equiv.asp it should if i understand it correctly immediately refresh page with the new value of add and with this I am aiming to change the text.
As code goes: If I run it, it will immediately display button with text Remove from shopping cart(when I show bool it show it as 1), but when i click at it it just do nothing
Anybody know what is the issue?