0

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?

1 Answers1

0

You can use JS to set new value to the session and reload only your cart without refreshing whole page.

Also you can convert your php code to blade format @if you want.

        <?php
        $bool = Session::get('add');
        if($bool == true) {
        $text = "Remove from shopping cart";
        } else {
        $text = "Add to shopping cart";
        }
       ?>
       <div class="" id="cart">
       <input type="button" class="corner" id="test" value="<?php echo $text; ?>">
        </div>            

            <script>
            $(document).ready(function () {          
            $('#test').on('click', function () {
            var test = this.value;
            if(test == "Add to shopping cart")
            {
            sessionStorage.setItem("add", true);
            $('#cart').load(document.URL +  ' #cart');
            } else {
            sessionStorage.setItem("add", false);
            $('#cart').load(document.URL +  ' #cart');
            }
            });
            });
            </script>
EHF Shahab
  • 700
  • 4
  • 14
  • I copy paste your code and I still have same issue with difference that in beginning it is showing Add to shopping cart and not Remove from shopping cart. –  Aug 10 '22 at 11:07
  • Try now please. – EHF Shahab Aug 10 '22 at 11:10
  • Still. In the warnings it is writing that class SESSION is not defined, but if i change it back to my $_SESSION["add"]; it just again change to Remove from shopping cart. And for some reason it is suggesting to add third equal mark in if statement –  Aug 10 '22 at 11:18
  • add use session; in the top of your controller – EHF Shahab Aug 10 '22 at 11:20
  • I am sorry I feel that the import thing cause just my lack of information about laravel in general, but I have 4 classed named Session and for none of them the warning will immediately just vanish. –  Aug 10 '22 at 11:40
  • Cookie is not bad choice too. – EHF Shahab Aug 10 '22 at 11:48
  • I finally import it and the warning is gone. In the if line is warning Comparison test == "Add to shopping cart" may cause unexpected type coercion and it will dissapear when I add 3 equal mark. –  Aug 10 '22 at 11:58