Call a php function from inside echo onclick without JS

Looked through some other peoples questions however the replies suggest using JS; however due to the parameters for this project; I don't believe thats viable for me.
Do not use JavaScript/CSSto hide/track/manipulate your data.
anyways I'm echoing this in side of a <?php ?> block:

        <a href="" onclick="addToRecentlyViewed("'.$row["productName"].'")">
                                            <img class="img-rounded img-thumbnail" src="products/' . $row["picture"] . '"/>
                                        </a>

And above it I have another <?php ?> block that has:

function addToRecentlyViewed($productName){
                
                // If the cookie is already set lets work with whats in there.
                if( isset($_COOKIE['recentlyViewedProducts'])){
                    $products = explode(',',$_COOKIE["recentlyViewedProducts"]);
                    
                    // Max of 4 items in the list.
                    if(sizeof($products) >= 4){
                        // Start -> red, green, blue, yellow and adding purple
                        //$products[0] = $products[1];
                        //$products[1] = $products[2];
                        //$products[2] = $products[3];
                        //$products[3] = $productName;
                        //end -> green, blue, yellow, purple.
                        //$newList = join(",",$products);
                    }
                    // If less than 4 then just add.
                    else{       
                        // Just append to list.
                        //$newList = $_COOKIE["recentlyViewedProducts"];
                        //$newList .= ',' . $productName;
                        //setcookie["recentlyViewedProducts", $newList];
                    }
                }
                // If its not set lets set up the cookie.
                else{
                    echo ' HELLO! ';
                    setcookie('recentlyViewedProducts', $productName);
                }
            }

I can't even get it to echo ' HELLO! ' so I'm thinking I might be doing something wrong :(



Read more here: https://stackoverflow.com/questions/68481083/call-a-php-function-from-inside-echo-onclick-without-js

Content Attribution

This content was originally published by Coding_Noob at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: