提问人:Coding_Noob 提问时间:7/22/2021 最后编辑:Reza RahemtolaCoding_Noob 更新时间:8/3/2021 访问量:59
在没有 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);
}
}
我什至无法做到这一点,所以我想我可能做错了什么:(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的规则,是吗?
答:
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好运
评论
onclick
onclick
onclick
是一个客户端事件,因此仅适用于 JS(客户端执行)。如果要触发PHP脚本,则需要使用普通链接(将PHP文件的URL放在链接中),单击该链接时会将客户端重定向到该页面。如果您不希望重定向客户端,则可以添加带有 id 的 iframe(宽度和高度设置为 0),并将链接目标设置为该 iframe。href
onclick
href
GET
<script>