2012-01-27 2 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> 

내 페이지의 수량 필드 및 장바구니 버튼이 있습니다. 구매자가 수량 필드를 입력하지 않고 장바구니에 추가 버튼을 클릭하면 오류가 팝업됩니다. 그 때문에 나는 다음과 같은 자바 스크립트 코드를 사용했다.

function form_quantity(val){ 


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


} 

하지만 작동하지 않습니다. 나는 그 오류를 추적 할 수 없었다. 코드를 수정하려면 어떻게해야합니까?

당신은 따옴표로 문자열 리터럴있어
+0

'if (! entriesqty)'를 할 수는 있지만 질문에 대한 답변이 아닐 수도 있습니다. 값을 확인하려면'alert (enteredqty)'를 추가하십시오. 또한 콘솔에 javascript 오류가 있는지 확인하십시오. – xbonez

답변

1

: 사실 당신이 무슨 짓을했는지,

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

을 (당신이 당신의 오류를 추적 할 수 말할 때 :

alert(Please enter quantity); 

당신은 말할 필요 Chrome을 사용하는 경우 디버깅 도구가 내장되어 있거나 Firefox의 경우 Firebug를 다운로드 할 수 있으며이 도구는 이와 같은 오류에 대해 쉽게 알 수 있습니다.)

그리고 다음 질문을 한 번 선결하려고합니다. 위의 오류를 수정하면 양식에 여전히 양식이 표시되지만 알림을 찾을 수 있습니다. 원하지 않는 경우는 앞서 가고 제출하여 form_quantity() 함수의 결과를 반환하기 위해 onclick을 업데이트하고 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

+0

감사. 방금 다음 호를 해결했습니다. 고마워. – designersvsoft

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