在没有 JS 的情况下从 echo onclick 内部调用 php 函数

Call a php function from inside echo onclick without JS

提问人:Coding_Noob 提问时间:7/22/2021 最后编辑:Reza RahemtolaCoding_Noob 更新时间:8/3/2021 访问量:59

问:

浏览了其他一些人的问题,但是回复建议使用 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);
    }
}

我什至无法做到这一点,所以我想我可能做错了什么:(echo ' HELLO! '

添加了一些代码:

</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的规则,是吗?

php 函数 参数传递

评论

1赞 Constantin Groß 7/22/2021
您不能通过处理程序执行 PHP 函数。PHP 在服务器端运行,JavaScript 在客户端运行。 属性用于 JavaScript 处理程序。onclickonclick
1赞 M. Eriksson 7/22/2021
onclick是一个客户端事件,因此仅适用于 JS(客户端执行)。如果要触发PHP脚本,则需要使用普通链接(将PHP文件的URL放在链接中),单击该链接时会将客户端重定向到该页面。如果您不希望重定向客户端,则可以添加带有 id 的 iframe(宽度和高度设置为 0),并将链接目标设置为该 iframe。href
1赞 Constantin Groß 7/22/2021
这就是为什么你不应该使用!;-)正如@MagnusEriksson所说:使用 the 传递一个你可以做出反应的参数onclickhrefGET
1赞 M. Eriksson 7/22/2021
谁给了你所有这些荒谬的限制,目的是什么?如果他们把你的手绑在背后,你就不能指望你能够使用它们。这些限制基本上阻碍了你做你所要求的事情。
1赞 Constantin Groß 7/22/2021
这些标签也是 JavaScrpipt,你不应该使用...... ;-)<script>

答:

0赞 Coding_Noob 7/22/2021 #1

感谢 @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好运