简体   繁体   English

单击链接后警报不显示

[英]Alert not showing after clicking link

I have created a Buy Now link that pulls the URL from my phpmyadmin database. 我创建了一个立即购买链接,从我的phpmyadmin数据库中提取URL。 Clicking the link adds the product to the cart but I would not like the user to go to checkout when they click the link, but instead stay on the same page and pop an alert saying 'Product was added to cart'. 单击该链接会将产品添加到购物车,但我不希望用户在单击链接时前往结帐,而是保持在同一页面上并弹出警告“产品已添加到购物车”。 Below is my current script but the alert does not show up and the user goes to the link. 下面是我当前的脚本,但警报未显示,用户转到该链接。 Can anyone tell me what is wrong? 谁能告诉我有什么问题? Thanks! 谢谢!

<script>
    $("#shop").onclick(function(event){
        $.post( '" . $producturl[0]->URL . "' );
        event.preventDefault();
        event.stopPropagation();
        console.log('Added To Cart');
    });
</script>


<?php $producturl = ProductURL::find(array('Product'=>$tube1->Tube1));
    if($producturl[0]->URL!=NULL){
        echo '<a href="' . $producturl[0]->URL . '" data-role="button" data-inline="true" data-mini="true" data-theme="d" target="vspage" onclick="' . "_gaq.push(['_trackEvent', 'Buy Now', 'Tube', '" . $tube1->Tube1 . "'" . "]); _gaq.push(['_link', '" . $producturl[0]->URL . "']);" . '" id="shop">Test Cart</a>';

        echo '<a href="#" id="shop">Test2 Cart</a>';
    }
?>

$producturl[0]->URL is the array that pulls the current product to cart from phpmyadmin $ producturl [0] - > URL是将当前产品从phpmyadmin拉到购物车的数组

Try deleting the anchor so your text is no longer a link, then make it a span with the shop id: 尝试删除锚点,使您的文本不再是链接,然后使其成为shop ID的范围:

<?php $producturl = ProductURL::find(array('Product'=>$tube1->Tube1));

        if($producturl[0]->URL!=NULL){
            echo '<span id="shop">Test Cart</span>';
        }
?>

Use 采用

window.alert('Added To Cart') 

instead of 代替

console.log('Added To Cart')

You should have jQuery and change to: 你应该有jQuery并改为:

    $( document ).ready(function() {
        $("#shop").click(function(event){ 

          event.preventDefault();

          // Your code here

          // Alert user
          alert('Added To Cart'); 

       });
   });

And you should have only one unique id on same page! 并且您应该在同一页面上只有一个唯一ID! You have two shop ids. 你有两个shop ID。

Update: 更新:

You have this too: 你也有这个:

$.post( '" . $producturl[0]->URL . "' );

To use php variables in javascript you should echo them, like: 要在javascript中使用php变量,你应该回应它们,例如:

$.post( '<?php echo $producturl[0]->URL;?>' );

I don't know what that $producturl[0]->URL variable have it, but this is not enought to update your cart, maybe you don't need this $.post to if you don't want to make anything from server side right now... perhaps you just want to update customer cart, like total price, quantity, add the new product... 我不知道$producturl[0]->URL变量有什么,但这不是更新你的购物车,也许你不需要这个$.post如果你不想做任何东西服务器端现在...也许您只是想更新客户购物车,如总价格,数量,添加新产品...

But if you really need to do something from server side at this moment, check $.post documentation HERE 但是,如果此时您确实需要从服务器端执行某些操作,请在此处查看$.post文档

<?php $producturl = ProductURL::find(array('Product'=>$tube1->Tube1));
    if($producturl[0]->URL!=NULL){      
echo '<a href="#shop" id="shop">Test2 Cart</a>';
}
    ?>

<script type="text/javascript">
$(document).on('click', '#shop', function (event) {
$.post( '" . $producturl[0]->URL . "' );
event.stopPropogation();
event.preventDefault();
alert('Product has been added to cart');
});
</script>

This is my current code now where the alert finally pops up but the cart itself when I go to the page does not update. 这是我现在的代码,其中警报最终会弹出,但当我转到页面时,购物车本身不会更新。 I used $.post , any help for this last step woud be great! 我使用$.post ,这最后一步的任何帮助都会很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM