2015-10-15 97 views
-2

「嗨,我有一個問題在這裏,我想知道如何我可以把條件放在這個代碼,如果我想設置按鈕被禁用,如果數量低於或等於0我輸入下面的代碼,謝謝你希望你能幫助我。「php代碼爲購物車

<?php 
$results = $mysqli->query("SELECT product_code, product_name, product_desc, product_img_name, QtyOnHand, product_price FROM allitems where id <= 4"); 
if($results){ 
$products_item = '<ul class="products">'; 
while($obj = $results->fetch_object()) 
{ 
$products_item .= <<<EOT 
<li class="product"> 
<form method="post" action="../../cart_update.php"> 
<div class="product-content"><h3>{$obj->product_name}</h3> 
<div class="product-thumb"><img src="../../images/{$obj->product_img_name}" class="img"></div> 
<div class="product-info"> 
Price {$currency}{$obj->product_price} 
<fieldset> 
<label> 
Quantity Left: {$obj-> QtyOnHand}<br> 
<span>Quantity</span> 
<input type="text" size="2" maxlength="2" name="product_qty" value="1" /> 
</label> 
</fieldset> 
<input type="hidden" name="product_code" value="{$obj->product_code}" /> 
<input type="hidden" name="type" value="add" /> 
<input type="hidden" name="return_url" value="{$current_url}" /> 
<div align="center"> 
<div align="center"> 
<button type="submit" class="add_to_cart" name="add_button" >Add</button></div> 
</div></div> 
</form> 
</li> 
EOT; 
} 
$products_item .= '</ul>'; 
echo $products_item; 
} 
?> 
+0

你爲什麼不使用HTML'min'屬性,並將其設置爲1,以便數量將永遠超越1. HTTP://www.w3schools .com/tags/att_input_min.asp – madforstrength

+2

您仍然需要驗證服務器上的輸入。對於客戶端,使用javascript:綁定一個函數來檢查輸入值到'onchange'或相同輸入的類似事件。在此功能內根據檢查結果切換按鈕狀態。 –

+0

對不起,但我的意思是,如果手頭的數量(QtyOnHand)小於或等於零,該按鈕將被禁用。 –

回答

0

您可以使用這樣的事情:

<input type="text" size="2" maxlength="2" {if($obj->QtyOnHand<1, 'disabled="disabled"')} name="product_qty" value="1" />