aphextwix
aphextwix

Reputation: 1858

Disable input element based on PHP variable value

How would I disable specific input elements based on the value of a PHP variable?

Example HTML

<?php 

$stock_count = 1; 

?>

<td width="252" valign="top">
          <a href="action-sale.php?size=<?=$size_quantity?>_con_b&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&amp;brand=<?=$product['brand_id']?>&amp;title=<?=$product['title']?>">
          <input name="button5" type="submit" class="submit-button-green"
                 id="sale" value="CONCESSION PRODUCT SOLD" style="width:100%" onclick="this.disabled=1;"/>
          </a>
        </td>
        <td align="right">
            <a href="action-delivery.php?size=<?=$size_quantity?>_con_b&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>">
              <input name="button" type="submit" class="submit-button"
                     id="delivery" value="PRODUCT DELIVERED" style="width:170px" onclick="this.disabled=1;"/>
              </a>
        </td>

Condition

    IF ($stock_count <= 0) 
{
    Disable ID 'Sale' 
}
ELSE IF ($stock_count > 0) 
{
    Do not disable anything
}

So based on the condition above the disabled attribute would be added to the sale input button. If the value of the $stock_count variable is greater than 0 than no action will be needed.

Any advice appreciated. Thanks

UPDATE

I managed to solve the issue with the following code:

 $disabled = $stock_count > 0 ? "" : "disabled";

 <input name="sale" type="submit" class="submit-button-green" 
                 id="sale" value="PRODUCT SOLD" style="width:100%"
                 onclick="location.href='action-sale.php'"; <?php echo $disabled; ?> />

Upvotes: 3

Views: 16561

Answers (5)

Bob Mac
Bob Mac

Reputation: 359

I think the other answers are correct for this situation, but there is another way depending on what you need to do.

If there are more things to be done other than disabling for example, it may make sense to "echo" your php values directly into javascript variables then use js/jQuery to handle all manipulations.

<script type="text/javascript">
   var myJsVar = '<?php echo $myPHPVar; ?>';
   // do js/jQuery conditions here to disable or whatever else you need
   ....

Upvotes: 0

Suchit kumar
Suchit kumar

Reputation: 11859

<?php 

    $stock_count = 1; 
    $disabled = $stock_count <= 0 ? "disabled='disabled'" : "";

?>

<input name="button5" type="submit" id="sale" ... <?php echo $disabled; ?> />

Upvotes: 0

stevenll
stevenll

Reputation: 1025

Add this somewhere in your php block

<?php $disable = ($stock_count>0) ? '' : 'disabled="disabled"'; ?>

And then in you html you can do this :

<input <?php echo $disabled; ?> name="button5" type="submit" class="submit-button-green" id="sale" value="CONCESSION PRODUCT SOLD" style="width:100%" onclick="this.disabled=1;"/>

Upvotes: 0

adeneo
adeneo

Reputation: 318212

<?php 

    $stock_count = 1; 
    $disabled = $stock_count > 0 ? "disabled='disabled'" : "";

?>

<input name="button5" type="submit" id="sale" ... <?php echo $disabled; ?> />

Just echo the attribute or an empty string based on a condition.

Upvotes: 4

TBI
TBI

Reputation: 2809

Use if condition in textbox -

<?php if($stock_count <= 0) { echo "disabled='true'"; } ?>

Here is the code inside textbox

<input name="button5" type="submit" class="submit-button-green" id="sale" value="CONCESSION PRODUCT SOLD" style="width:100%" <?php if($stock_count <= 0) { echo "disabled='true'"; } ?> onclick="this.disabled=1;"/>

Upvotes: 0

Related Questions