geds13
geds13

Reputation: 191

How to disable button in an if condition

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

<?php
    $prod_qty = $row['prod_quantity'];

            if ($prod_qty == '0'){
                echo "<h1>sold out</h1>";
            }
?>

and here is the button that i need to be disabled when prod_qty == 0

<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />

Upvotes: 0

Views: 106874

Answers (5)

Jounior Developer
Jounior Developer

Reputation: 155

<?php
    $prod_qty = $row['prod_quantity'];

            if ($prod_qty == '0'){
                echo "<h1>sold out</h1>";
            }
?>

<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" {{($prod_quantity == 0) ? 'disabled' : ''}} />

Upvotes: 0

Sandun Harshana
Sandun Harshana

Reputation: 729

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"]?>)" />

Upvotes: 0

Kristian
Kristian

Reputation: 2505

<?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"]?>)" />
        }
?>

Upvotes: 1

Satish Sharma
Satish Sharma

Reputation: 9635

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 ';
  }
?>
 />

Upvotes: 2

Jenz
Jenz

Reputation: 8367

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"]?>)" />

Upvotes: 24

Related Questions