3

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; ?> />
3
  • w3schools.com/tags/att_input_disabled.asp Commented Aug 6, 2014 at 12:52
  • why have you tagged your question with javascript and jquery? According to your description, you only need php. Is there something else going on? Commented Aug 6, 2014 at 12:56
  • @andrew - I was expecting an answer that involved javascript when I posted the question initially. Now changed. Commented Aug 6, 2014 at 13:25

5 Answers 5

4
<?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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution. I don't seem to be getting the expected result as the inputs are still enabled when the condition is true. Please see my update above - any advice?
You'll have to look at the outputted HTML and make sure it's valid, and that you're getting what you're expecting. This is the way to add an attribute like this, just make sure the condition does what you want, and that that the HTML is outputted correctly.
0

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;"/>

2 Comments

I found more suitable the ternary operator here: <?php echo ($stock_count <= 0)? "disabled='true'" : ""; ?>
yeah, we can use that but here no need else condition
0

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;"/>

Comments

0
<?php 

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

?>

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

Comments

0

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
   ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.