2012-01-27 146 views
0

我已使用以下代碼在我的購物車網站上顯示產品詳細信息。javascript空字段驗證

<div id="inner_right"> 
<form name="product_form" id="product_form" method="post" onsubmit="form_quantity(<?php echo $productid; ?>);"> 
<input type="hidden" name="hidden_<?php echo $productid; ?>" id="hidden_<?php echo $productid; ?>" /> 

    <h1>Product Details of <?php echo $fetchproductname; ?></h1> 
    <div>&nbsp;</div> 
    <div id="product_left"><img src="<?php echo $path.$fetchimage; ?>" alt="" width="400" height="300" /></div> 
    <div id="product_right"> 
    <div><strong>Category Name:</strong> <?php echo $categoryname; ?></div> 
    <p><strong>Product Number:</strong> <?php echo $fetchproductno; ?></p> 
    <p><strong>Price:</strong> <span class="price">$<?php echo $fetchproductprice; ?></span></p> 
    <p><strong>Stock:</strong> <?php echo $fetchproductstock; ?> nos</p> 

    <?php 


    $select_quantity = "SELECT * FROM `tbl_cart` WHERE `intProductid` = '".$productid."' AND `intSessionid` = '".$globalsessionid."'"; 
    $select_quantity_res = mysql_query($select_quantity); 
    $sel_qty_num = mysql_num_rows($select_quantity_res); 

    $fetch_quantity = mysql_fetch_array($select_quantity_res); 


     $fetch_proid = $fetch_quantity['intProductid']; 
     $fetch_exqty = $fetch_quantity['intQuantity']; 
     ?> 
     <p><strong>Quantity:</strong> <input name="quantity_<?php echo $productid; ?>" id="quantity_<?php echo $productid; ?>" value="<?php echo $fetch_exqty; ?>" class="quantity" type="text" /></p> 




    <div class="submit"> 
     <button id="registerButton" type="submit">Add To Cart</button> 
    </div> 
    <input type="hidden" name="cart" id="cart" value="<?php echo $productid; ?>" /> 
    </div> 
    <div class="clear">&nbsp;</div> 

    </form> 
</div> 

在我的頁面中有一個數量字段,並加入購物車按鈕。如果買方點擊添加到購物車按鈕而沒有輸入數量字段,則會彈出錯誤消息。爲此,我使用了下面的JavaScript代碼。

function form_quantity(val){ 


var enteredqty = document.getElementById('quantity_'+val).value; 
if(enteredqty =='') 
{ 
alert(Please enter quantity); 
} 


} 

但它不起作用。我無法追蹤錯誤。我如何糾正我的代碼?

+0

你可以做'if(!enteredqty)',雖然這可能不是你的問題的答案。添加'alert(enteredqty)'以確保該值正在被拾取。還請檢查您的控制檯是否存在任何引發的JavaScript錯誤。 – xbonez 2012-01-27 06:33:02

回答

1

你已經有了一個字符串字面量沒有引號:

alert(Please enter quantity); 

你需要說:

alert("Please enter quantity"); 
// OR 
alert('Please enter quantity'); 

(當你說你無法跟蹤你的錯誤,究竟做了你試試吧?如果使用Chrome,它有內置的調試工具,或者Firefox可以下載Firebug,並且這些工具可以很容易地告訴你這樣的錯誤。)

爲了搶佔你的下一個問題,一次你修正了上面的錯誤,你會發現雖然警報顯示錶單仍然提交。您需要更新您的onclick來回報您的form_quantity()函數的結果,並返回false當你不想提交先走(即,當有一個驗證錯誤):

<form name="product_form" id="product_form" method="post" 
     onsubmit="return form_quantity(<?php echo $productid; ?>);"></form> 

<script> 
function form_quantity(val){ 
    var enteredqty = document.getElementById('quantity_'+val).value; 
    if(enteredqty === '') { 
     alert('Please enter quantity'); 
     return false; 
    } 
} 
</script> 

+0

謝謝。它正在工作。 – designersvsoft 2012-01-27 06:42:58

+0

謝謝。你也解決了我的下一個問題。非常感謝。 – designersvsoft 2012-01-27 06:55:30

1
function form_quantity(val){ 
    var enteredqty = document.getElementById('quantity_'+val).value; 
    alert(enteredqty);// check the givel value is right. 
    if(enteredqty ==''){ 
     alert("Please enter quantity");// double qute added. 
    } 
} 

選中此項,您可能會發現一些路徑。

+0

謝謝。它正在工作。 – designersvsoft 2012-01-27 06:43:49