基于PHP变量值禁用输入元素
如何根据PHP变量的值禁用特定的输入元素 示例HTML基于PHP变量值禁用输入元素,php,html,Php,Html,如何根据PHP变量的值禁用特定的输入元素 示例HTML <?php $stock_count = 1; ?> <td width="252" valign="top"> <a href="action-sale.php?size=<?=$size_quantity?>_con_b&id=<?=$product['product_id'];?>&barcode=<?=$barcode?
<?php
$stock_count = 1;
?>
<td width="252" valign="top">
<a href="action-sale.php?size=<?=$size_quantity?>_con_b&id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&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&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>
如果文本框中的条件为-
<?php if($stock_count <= 0) { echo "disabled='true'"; } ?>
将其添加到php块中的某个位置
<?php $disable = ($stock_count>0) ? '' : 'disabled="disabled"'; ?>
然后在html中可以执行以下操作:
<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;"/>
我认为其他答案对于这种情况是正确的,但是根据您需要做什么,还有另一种方法
例如,如果除了禁用之外还有更多的事情要做,那么将php值直接“回显”到javascript变量中,然后使用js/jQuery来处理所有操作可能是有意义的
<script type="text/javascript">
var myJsVar = '<?php echo $myPHPVar; ?>';
// do js/jQuery conditions here to disable or whatever else you need
....
var myJsVar='';
//在这里执行js/jQuery条件以禁用或执行您需要的其他操作
....
为什么要用javascript和jquery标记问题?根据您的描述,您只需要php。还有其他事情吗?@andrew-我最初发布这个问题时,正期待着一个涉及javascript的答案。现在改变了。我在这里找到了更合适的三元运算符:是的,我们可以使用它,但这里不需要其他条件谢谢你的解决方案。我似乎没有得到预期的结果,因为当条件为真时,输入仍然处于启用状态。请看我上面的更新-有什么建议吗?您必须查看输出的HTML并确保它是有效的,并且您得到了您所期望的。这是添加如下属性的方法,只需确保条件符合您的要求,并且HTML输出正确。
<?php
$stock_count = 1;
$disabled = $stock_count > 0 ? "disabled='disabled'" : "";
?>
<input name="button5" type="submit" id="sale" ... <?php echo $disabled; ?> />
<?php $disable = ($stock_count>0) ? '' : 'disabled="disabled"'; ?>
<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;"/>
<?php
$stock_count = 1;
$disabled = $stock_count <= 0 ? "disabled='disabled'" : "";
?>
<input name="button5" type="submit" id="sale" ... <?php echo $disabled; ?> />
<script type="text/javascript">
var myJsVar = '<?php echo $myPHPVar; ?>';
// do js/jQuery conditions here to disable or whatever else you need
....