如果链接包含此条件,则阻止打开

抱歉这个问题,我认为这并不困难,但我是 PHP 新手,我尝试通过链接显示一些产品图像,如果该项目可用$row['p_status']=='1'好的,链接将被打开,否则它会显示一个警报,但我尝试在 while 循环内编写一些代码,但是当我测试它时,它会根据这些项目的数量显示许多警报,如果我是 $row['p_status']=='0'有 5 个产品,如果我点击一个产品,它会显示五个警报,但它不可用。


请帮助我,这是我的代码。


<?php 

    $query = "SELECT * FROM products";

    $result = mysqli_query($conn,$query);                   

    while ($row = mysqli_fetch_assoc($result)) {

 ?>

    <div class="column">

            <img src="<?=get_image($row['image']);?>">

             <span>Add to Your Cart </span>

            <a href="mycart.php?add=<?=$row['product_id'];?>" id="a">

            <?php

            if ($row['pro_status']=='0') {

                # the link above not open but, how to do with code?

            }

            ?>

    </div>

<?php 

}

 ?>


跃然一笑
浏览 115回答 2
2回答

胡子哥哥

您需要一个 if 语句来检查状态是否为 1。如果是 1,您将显示带有 href 的链接,否则您将调用 onclick 事件来显示缺货警报。这是解决方案之一(对于初学者来说更容易):<?php&nbsp;&nbsp; &nbsp; $query = "SELECT * FROM products";&nbsp; &nbsp; $result = mysqli_query($conn,$query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; while ($row = mysqli_fetch_assoc($result)) {&nbsp;?>&nbsp; &nbsp; <div class="column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="<?=get_image($row['image']);?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if($row['pro_status'] == 1) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="mycart.php?add=<?=$row['product_id'];?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add to Your Cart&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } else { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button onclick="return alert('Out of stock!')">Add to cart</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; </div><?php&nbsp;}&nbsp;?>

幕布斯6054654

只需要让点击事件返回 false 即可。以下应该这样做:if ($row['pro_status']=='0') {&nbsp; &nbsp; $("#a").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; });}另一个解决方案是使用 PreventDefault()if ($row['pro_status']=='0') {&nbsp; &nbsp; $("#a").click(function(e){&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5