How to disable button in an if condition

How to disable button in an if condition

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Disabling Buttons Based on Inventory State in PHP Templates

You are attempting to conditionally disable an "Add to Cart" button when $prod_quantity equals zero. In many legacy or custom e-commerce platforms, the logic for handling inventory often resides directly within the template rendering layer, requiring a mix of server-side data evaluation and client-side event prevention.

The code snippet you provided reveals a common pattern in PHP-driven sites: retrieving product data from a database row and checking specific fields before rendering HTML. However, there is a critical distinction between displaying a message like "sold out" and actually preventing a user interaction. Your current implementation checks the condition correctly, but simply echoing an <h1> tag does not modify the behavior of the input element below it.

To disable the button dynamically based on that PHP condition, you must ensure the disabled attribute is present in the HTML output only when the inventory count is zero. The most robust way to achieve this in a server-rendered template is to inject the disabled attribute directly into the <input> tag using PHP logic.

Here is how you can refactor your button code:

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

This approach relies on the server-side rendering cycle. When $prod_qty is 0, the PHP block outputs disabled. The resulting HTML sent to the browser becomes:
<input type="button" value="Add to Cart" onclick="..." disabled>

The browser's default behavior interprets this attribute as a state change, preventing any click events from firing regardless of what happens inside the onclick handler. This is generally preferred over relying solely on JavaScript to disable an element because it communicates the state clearly in the DOM and respects accessibility standards better than hiding elements or removing attributes entirely.

There is, however, an important nuance regarding how you are checking the value in your original snippet: $prod_qty == '0'. You are comparing a variable (which might be an integer from the database) against a string '0'. While PHP often coerces types to make this work, it is best practice to ensure data consistency. If $row['prod_quantity'] comes directly from a numeric field in your database, it will likely be an integer. In that case, the comparison should strictly be:

if ($prod_qty == 0) { // No quotes around 0 if it's an integer
    echo 'disabled';
}

Mixing types can lead to unpredictable behavior if your data storage changes or if you switch between different database drivers that handle type casting differently. Always verify the actual data type returned by your query to ensure the condition evaluates as intended.

Another consideration is the separation of concerns. While embedding logic like <?php if ... ?> directly inside HTML attributes works for simple cases, it can quickly become difficult to maintain as the UI grows. If you are using a framework or a modern templating engine, passing a boolean flag (e.g., $isSoldOut) from your controller to your view and iterating over it might be cleaner than repeating the SQL condition in multiple places.

If you need to handle this without modifying the HTML attributes directly—for instance, if you must keep the button visually enabled but just stop the JavaScript function—you can use a JavaScript check. However, this requires the data to be accessible to the client-side script, often by echoing the value into a hidden field or passing it as a global variable, which introduces potential security risks if not sanitized properly. The attribute-based approach remains the safest default for server-rendered applications.

For developers working on Laravel projects specifically, you might find that integrating this logic with blade directives simplifies the syntax further. If your project is hosted or managed by a service like LaravelCompany, they often provide documentation on leveraging their specific components to handle state management more elegantly than raw PHP blocks in templates.

Ultimately, the decision to disable the attribute on the server side ensures that the user cannot bypass the restriction via developer tools unless the JavaScript logic itself is exposed. It aligns with the principle of defense-in-depth: rely on the server to enforce business rules regarding inventory availability rather than trusting client-side checks alone.

Our network