簡體   English   中英

在沒有 JS 的情況下從 echo onclick 內部調用 php 函數

[英]Call a php function from inside echo onclick without JS

瀏覽了一些其他人的問題,但答復建議使用 JS; 但是由於該項目的參數; 我不相信那對我來說是可行的。
不要使用 JavaScript/CSS 來隱藏/跟蹤/操縱您的數據。
無論如何,我在 <?php ?> 塊的一側回應了這一點:

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

在它上面我有另一個 <?php ?> 塊,它有:

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);
                }
            }

我什至不能讓它回聲“你好! '所以我想我可能做錯了什么:(

添加了一些代碼:

</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip()
    })
    
    $(function addToRecentlyViewed($productName){
        <?php
            // 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{
                setcookie('recentlyViewedProducts', $productName);
            }
            ?>
        }
    )
</script>

這確實創建了 cookie; 但它說它自動刪除了。 我應該使用無限時間嗎......? 另外我從來沒有用過這些
<script> $(function xxx){} </script>
我沒有違反無 JS 規則吧?

感謝@MagnusEriksson 和@ConstantinGroß 的幫助創建了一個單獨的頁面,因此它不是onclick 啟動功能而是重定向到一個設置cookie 的頁面,如下所示:

<?php 

    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);
                setcookie("recentlyViewedProducts",$newList);
            }
            //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 'woo hoo!';
            setcookie('recentlyViewedProducts', $productName);
        }
        
    }
    
    addToRecentlyViewed($_GET["productName"]);
    header('location: product.php?product='.$_GET["productName"]);
?>

祝大家好運:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM