How to disable button in an if condition

前端 未结 4 2085
我在风中等你
我在风中等你 2021-01-17 06:56

How do i disable a button if the prod_quantity == 0?



        
相关标签:
4条回答
  • 2021-01-17 07:09
    <?php
    $prod_qty = $row['prod_quantity'];
    
            if ($prod_qty == '0'){
                echo "<h1>sold out</h1>";
                echo '
                    <input 
                        type="button" 
                        value="Add to Cart" 
                        onclick="addtocart('.$row['prod_id'].')" 
                        disabled
                    />'
            }else{
                <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
            }
    ?>
    
    0 讨论(0)
  • 2021-01-17 07:11
    if($prod_qty==0)
    {
         <?php
         <input type="button" value="Add to Cart" disabled />
         <?php
    }
    

    UPDATE 2 :

    <input type="button" value="Add to Cart" 
      <?php if($row["prod_qty"]==0) 
      {
         echo ' onclick="addtocart('.$row["prod_id"].')" ';
      }
      else
      {
           echo ' disabled=disabled ';
      }
    ?>
     />
    
    0 讨论(0)
  • 2021-01-17 07:11

    try this

    javascript onclick function...

    function addtocart(prod_qty){
        var quantity=parseInt(prod_qty);
        if (quantity == 0){
             document.getElementById("addtocartButton").disabled = true;   
        }
    
    }
    

    your button

    <input type="button" value="Add to Cart" id="addtocartButton" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
    
    0 讨论(0)
  • 2021-01-17 07:19

    Add an if condition to the button to set the property as disabled if $prod_qty == '0' like shown below :

    <input type="button" value="Add to Cart" <?php if ($prod_qty == '0'){ ?> disabled <?php   } ?> onclick="addtocart(<?php echo $row["prod_id"]?>)" />
    
    0 讨论(0)
提交回复
热议问题